C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Should flowing off the end of a value-returning function be
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Ian
Guest





PostPosted: Wed Sep 07, 2005 3:39 am    Post subject: Should flowing off the end of a value-returning function be Reply with quote



Section 6.6.3.2 was brought to my attention on a thread over on
comp.lang.c++.

"flowing off the end of a function is equivalent to a return with no
value; this results in undefined behaviour in a value-returning function".

Should this undefined behaviour be promoted to a compile time error?

Ian




---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Victor Bazarov
Guest





PostPosted: Wed Sep 07, 2005 1:54 pm    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote



Ian wrote:
Quote:
Section 6.6.3.2 was brought to my attention on a thread over on
comp.lang.c++.

"flowing off the end of a function is equivalent to a return with no
value; this results in undefined behaviour in a value-returning
function".
Should this undefined behaviour be promoted to a compile time error?

Why should it? Compile time errors are only about ill-formed code.
What if that function is never called? What if the return value is
never used? It's like dereferencing a null pointer. What if it is
done in a part of code that never gets executed?

#include <stdio.h>
int foo() { printf("abc"); }
int bar() { int *p = 0; return *p; }
int main() {}

V


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
John Nagle
Guest





PostPosted: Wed Sep 07, 2005 1:54 pm    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote



Ian wrote:

Quote:
Section 6.6.3.2 was brought to my attention on a thread over on
comp.lang.c++.

"flowing off the end of a function is equivalent to a return with no
value; this results in undefined behaviour in a value-returning function".

Should this undefined behaviour be promoted to a compile time error?

Of course it should be an error.

But it won't be changed. That would break existing code.

John Nagle
Animats

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Maciej Sobczak
Guest





PostPosted: Wed Sep 07, 2005 1:54 pm    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote

Ian wrote:

Quote:
"flowing off the end of a function is equivalent to a return with no
value; this results in undefined behaviour in a value-returning function".

Should this undefined behaviour be promoted to a compile time error?

It should be probably clarified.
Flowing off the end is equivalent to a return with no value *only* if
the function is declared as returning void - and this part is missing in
the standard. If the function returns something else, then return with
no value would be illegal (earlier in 6.6.3/2 and I think diagnostic is
required) and therefore it is not true that flowing off the end is
equivalent to it.

In general, flowing off the end cannot be reliably detected at compile
time due to exceptions that can prevent the control from flowing off the
end:

int foo()
{
bar();
} // flowing off the end? no, if bar throws


--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
chris jefferson
Guest





PostPosted: Wed Sep 07, 2005 1:55 pm    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote

Ian wrote:
Quote:
Section 6.6.3.2 was brought to my attention on a thread over on
comp.lang.c++.

"flowing off the end of a function is equivalent to a return with no
value; this results in undefined behaviour in a value-returning function".

Should this undefined behaviour be promoted to a compile time error?


I'm sure I won't be the only person to reply to this. The problem is
that it is undecidable to decide if a function will reach the end or
not. At the moment different compilers do different amounts of deduction
to try to give helpful advice, but it's not possible in general. We
could impose some very simple deductions a compiler must make to check,
and if not there must be an obvious final return value. That would
however force people to bloat code which they don't have to. Consider
the following cases, what do you think behaviour should be? (I've seen
code like both of these in real code, although obviously these are
massively simplified

int foo()
{
while(1)
{
if(g()) return 1;
if(h()) return 2;
}
}


int foo(int i, int j)
{
if(i) return 0;
if(j) return 1;
}

int foo(int i)
{
if(i>0) return 0;
if((i-1)<0) return 1;
}


One thing I would like to see, and I'm suprised no compiler seems to
have, is a way of adding an "abort()" to the end of each function which
should return where the compiler can't deduce that the function always
returns correctly, as a way of catching some errors.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Michael Karcher
Guest





PostPosted: Thu Sep 08, 2005 4:13 am    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote

Ian <ian-news (AT) hotmail (DOT) com> wrote:
Quote:
"flowing off the end of a function is equivalent to a return with no
value; this results in undefined behaviour in a value-returning function".

Should this undefined behaviour be promoted to a compile time error?

I also thought this way some time ago, especially in functions returning
objects with a non-trivial destructor, but the problem is code like this:

// returns the string value of a bool-with-error (-1: error flag) given
// as argument.
std::string foo(int a)
{
switch(a)
{
case 0:
return "false";
case 1:
return "true";
case -1:
return "ERROR";
}
}

This code does fail horribly if a is not in the correct range, because of
the undefined behaviour you cited. But do you really want to make code
like this, which does exist, illegal?

A good solution to this question is, of course, ensuring the precondition,
or make the code fail in a non-undefined-behaviour-way like replacing the
"case -1:" by "default:". But this still implies that the compiler does
flow-tracking and knows that you can not get past the switch statement. An
easier-to-track version would have the error case after the switch, so there
is no falling-off-the-edge.

So, I have a counter-proposal: Instead of making it illegal, compile the
code "as if" the procedure ended in
'assert((__func__ " should return a value ",0));'.

If the compiler supports flow-tracking and figures out that this line can
not be reached, you get current behaviour with zero overhead; if the line
cannot be reached, but the compiler does not deduce it, you get a little
overhead and hopefully a compiler warning, telling you about it. If the
line really can be reached you replace undefined behaviour by defined program
termination, which is a good thing, IMHO.

Michael Karcher

Michael Karcher

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
ThosRTanner
Guest





PostPosted: Thu Sep 08, 2005 4:20 am    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote

I think an addendum to my previous email didn't make it. If it appears
twice, I apologise.

There are obviously more subtle ways of confusing the compiler than
mentioned in my previous post. Any compiler with an optimiser should be
able to determine one of the following.

1) The function never drops off the end - not an error
2) The function always drops off the end - should generate an error
3) The function might drop off the end

The 3rd case is contentious. My experience with lint indicates that
"might drop off the end" is generally the result of program flow being
dependant on a a chain of variables being set as the result of
something like (and this is somewhat simplified):

bool first_time = true;
bool some_logical_value;
do {
do some processing
if (first_time) { some_logical_value = true };
do some more processing
if (some_good_reason) { return a_value };
} while (some_logical_value);

This sort of stuff can be a nightmare to maintain, and I've found it
better to try and get that sort of mutual dependancy out of the loop.
Really, if the compiler can't tell whether or not some point in the
code is reached, how is the maintainer going to?

That's why I'd prefer it to be an error, though I rather anticipate
other people will have different views to me.

(This could be put forward as another argument for SESE I suppose).

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Vladimir Marko
Guest





PostPosted: Thu Sep 08, 2005 4:20 am    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote

Ian wrote:
Quote:
Section 6.6.3.2 was brought to my attention on a thread over on
comp.lang.c++.

"flowing off the end of a function is equivalent to a return with no
value; this results in undefined behaviour in a value-returning function".

Should this undefined behaviour be promoted to a compile time error?

int foo(){
bar();
}

Without analyzing the definition of bar the compiler can not prove that
the control flow shall reach the end of foo (bar may terminate the
program or throw an exception, the latter being a reasonable way of
error reporting). Even if the definition of bar would be visible the
proof may not exist -- it may for example depend on run-time parameters
controlled by a different program. That's why such code is well-formed.

Do you want to make reasonable code ill-formed?

And what about this:

int report_error(int x){
assert(::throwing);
report_error_core_(x); // throws if ::throwing, returns otherwise
}

Should it compile in debug mode and fail to compile in release mode?
(I know this is an artificial example but something similar may actualy
be used.)

Vladimir Marko

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
msalters
Guest





PostPosted: Thu Sep 08, 2005 4:20 am    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote


Ian schreef:

Quote:
Section 6.6.3.2 was brought to my attention on a thread over on
comp.lang.c++.

"flowing off the end of a function is equivalent to a return with no
value; this results in undefined behaviour in a value-returning function".

Should this undefined behaviour be promoted to a compile time error?

How do you prove at compile time that a function flows off the end?
In general, that requires solving the halting problem, plus you can't
see into other functions.

char* foo( char* bar )
{
do {
if ( !*bar ) return bar;
} while( bar++ );
}

This is legal if bar is null-terminated. In fact, strlen is often
implemented similarly. However, the compiler can't determine at
compile time whether this will flow off the end.

HTH,
Michiel Salters

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
ThosRTanner
Guest





PostPosted: Thu Sep 08, 2005 4:20 am    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote

Addendum to my previous post: Yes, of course there are other ways of
confusing the compiler. But compilers are capable of a reasonable
amount of flow analysis (for optimising out dead code if nothing else),
and they can deduce one of the following:

1) The end of the function can never be reached - no problem
2) The end of the function will be reached - should be an error
3) The end of the function might be reached - should require a
diagnostic. I'd prefer an error, because if the compiler can't tell,
how can a maintainer?

I've seen a few of the last using lint and working out whether or not
the code will drop through or not because of variables that depend on
other variables causing a return before the end of the function is
quite hairy.

Another argument for SESE....

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
ThosRTanner
Guest





PostPosted: Thu Sep 08, 2005 4:20 am    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote


Ian wrote:
Quote:
Section 6.6.3.2 was brought to my attention on a thread over on
comp.lang.c++.

"flowing off the end of a function is equivalent to a return with no
value; this results in undefined behaviour in a value-returning function".

Should this undefined behaviour be promoted to a compile time error?
Well, yes, although it's a little difficult for the compiler to tell

all the time. For instance

int wibble(int a, int b, int c)
{
while (some_condition_that_can_never_be_true_with_these_args(a, b,
c))
{
if (fed_up()) return 99;
}
//You know you can never get here, but the compiler doesn't
}

Under the circumstances I still think it should error, as either the
function always returns false (in which case, the function should be
void and you would code the loop differently), or the arguments might
possibly be different one day, and the function could unexpectedly
return true, and then where would you be?

I'm pretty sure other people will disagree with this though.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
kanze
Guest





PostPosted: Thu Sep 08, 2005 4:20 am    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote

Ian wrote:
Quote:
Section 6.6.3.2 was brought to my attention on a thread over on
comp.lang.c++.

"flowing off the end of a function is equivalent to a return
with no value; this results in undefined behaviour in a
value-returning function".

Should this undefined behaviour be promoted to a compile time
error?

How can a compiler detect the problem (with no false positives)?

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Ron Natalie
Guest





PostPosted: Thu Sep 08, 2005 4:21 am    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote

Maciej Sobczak wrote:

Quote:

It should be probably clarified.
Flowing off the end is equivalent to a return with no value *only* if
the function is declared as returning void - and this part is missing in
the standard. If the function returns something else, then return with
no value would be illegal (earlier in 6.6.3/2 and I think diagnostic is
required) and therefore it is not true that flowing off the end is
equivalent to it.

Not true. The standard is quite specific:


"Flowing off the end of end of a function is equivelent to return with
no value; this results in undefined behavior in a value returning
function. (6.6.3).

No diagnostic is required for undefined behavior.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Ron Natalie
Guest





PostPosted: Thu Sep 08, 2005 4:22 am    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote

Ian wrote:
Quote:
Section 6.6.3.2 was brought to my attention on a thread over on
comp.lang.c++.

"flowing off the end of a function is equivalent to a return with no
value; this results in undefined behaviour in a value-returning function".

Should this undefined behaviour be promoted to a compile time error?


It's not a compile time error because it's not easy to tell if the
program will flow off the end in all circumstances. A compiler that
can tell is free to generate extra diagnostics (and many do).


For example:

int foo() {
while(some_condition()) {
if(some_other_condition())
return 1;
}
}

does some_other_condition return true before some_condition ever returns
false?

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Ian
Guest





PostPosted: Thu Sep 08, 2005 4:22 am    Post subject: Re: Should flowing off the end of a value-returning function Reply with quote

John Nagle wrote:
Quote:

Should this undefined behaviour be promoted to a compile time error?

Of course it should be an error.

But it won't be changed. That would break existing code.

Break existing broken code, interesting concept!


Ian

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.