|
![](/i/fill.gif) |
Perhaps more alarming than anything I read in a book: Apparently in C++
it is legal for a function to not have a return statement.
As in, I declared my function as returning a value. I ran the program
and printed out the result of the function. I got a segfault. I changed
the code a bit, and ran it again. This time, it printed garbage, and
/then/ it segfaulted.
When I looked at my function, I found I'd written "it->second;" rather
than "return it->second;". Not only is this apparently legal, it doesn't
even generate a compile-time /warning/.
(Contrast this with Java. If the compiler cannot statically prove that
every single possible program branch ends with a return or a throw, it
point-blank refuses to compile your class. It gives you warnings if code
is unreachable. It even complains if you have a void function that uses
return just to exit early, and there's nothing afterwards...)
Post a reply to this message
|
![](/i/fill.gif) |