POV-Ray : Newsgroups : povray.off-topic : Teach yourself C++ in 21 days : Re: Teach yourself C++ in 21 strange malfunctions Server Time
29 Jul 2024 10:19:46 EDT (-0400)
  Re: Teach yourself C++ in 21 strange malfunctions  
From: Invisible
Date: 17 Apr 2012 06:16:31
Message: <4f8d42ff$1@news.povray.org>
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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.