Thread: Pointless Warning?

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Other than the excuse of it being too much work for the compiler to check (which I think it a pretty lousy excuse)
    With "too much" I mean "too much to be done before the sun explodes", not "too much for those lazy compiler writers to implement". Proving anything non-trivial about an imperative program is nigh impossible.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by anon View Post
    Well, for one thing assert would not be there if NDEBUG is defined...
    Since NDEBUG was not mentioned, I assumed it was not defined.

  3. #18
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    With "too much" I mean "too much to be done before the sun explodes", not "too much for those lazy compiler writers to implement". Proving anything non-trivial about an imperative program is nigh impossible.
    That's a problem for the optimizer to worry about. If you compile without otimizations, it should only look inside the current function to see if a code branch can be executed.
    I've seen warnings in VC++ in Release mode that says "code cannot be reached", even though visually it looks like it can, but I guess the optimizer looks through the entire program to see if a certain condition can ever be true... I find that particular warning to be a bit annoying though, since it may not be possible to reach that code today, but who knows about tomorrow?

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's a problem for the optimizer to worry about.
    It's a matter of compile time. You'd have to take a huge hit - and I mean HUGE - in compile time so the compiler can derive it's proof.
    Did I mention that the hit is HUGE? In a large program, I'm talking about going from minutes to days of compile time, and memory requirements that would make a modern hard drive shiver.

    Of course, if any of the relevant code is dynamically linked, you're out of luck anyway, because you can't prove anything about code that's not there.

    The optimizers are smart, especially the dead code eliminators, but they're not omniscient. It's unreasonable to put such requirements in the standard.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well like I said, I don't want the optimizer to look through the entire program to prove a fact that could easily change the next time you edit any source file in the program; I usually disable that particular warning anyways. I'm just saying it should check in the current function to make sure that if the function is supposed to return a value, that it has a return statement either at the end of the function, or at the end of each code branch (such as if, else if, else...)

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    I'm just saying it should check in the current function to make sure that if the function is supposed to return a value, that it has a return statement either at the end of the function, or at the end of each code branch (such as if, else if, else...)
    So, you are saying that even though the programmer can prove that control will never reach the end of the function body, the programmer must put in a dummy return value there anyway? For example, consider a loop that returns from within the loop body but is otherwise an infinite loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    So, you are saying that even though the programmer can prove that control will never reach the end of the function body, the programmer must put in a dummy return value there anyway? For example, consider a loop that returns from within the loop body but is otherwise an infinite loop.
    Sure, why not? All you need to do is put a return statement at the end of the function, and it should handle all situations. Although in some cases a throw might be more appropriate. If you don't think it should ever reach that line, you can put a comment there (and maybe an assert) so others know why it's there.

    If the compiler determines that it isn't required, it can optimize that code away, so it won't affect performance at all, it's just for readability and consistency.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Sure, why not? All you need to do is put a return statement at the end of the function, and it should handle all situations. Although in some cases a throw might be more appropriate. If you don't think it should ever reach that line, you can put a comment there (and maybe an assert) so others know why it's there.
    I find the practice dubious: the return statement would be unreachable code and would obscure the proof that the line is logically unreachable. If we take Sutter and Alexandrescu's recommendation to prefer assertions to exceptions for programming errors, throwing an exception would not be ideal either since control reaching that line indicates a programming error.

    Consequently, I think that an assertion would be better than both returning a dummy value and throwing an exception, though the compiler should then not warn that the assertion is unreachable, if it is able to determine as such (that should be the case anyway since the assertion would be ignored in a release build with optimisations turned on).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM