19

If you are programming in C++ please always return a type from a function as the function prototype return type.

I had to debug some old code that failed to return a boolean on one of its flow paths. It would work on linux in release and debug, would work on windows in debug, and fail on windows in release.

The failure was NOT straightforward at ALL. It would return exit code 3. Then if I added a debug print to the function it would segfault.

Why the hell would popping something extra off the return/call stack not crash more readily? Wth is the point of debug compile if it won't catch shit like this?

Comments
  • 19
    There are compiler flags to detect that and either display it as a warning or error.

    Why this is still opt-in is beyond me...
  • 3
    When i built the core of a game engine a few years back, i had exactly the same Problem. First thought some reference to the Engine Class was broken, because i used it across multiple Threads (well, thread safe ofcourse), but in the end it turned out, that the Threads all use a function to get the reference. That function returns the ref by signature, but in fact did not

    and left me wondering, why the reference always crashed the thread.

    Two days i was busy figuring out, what the cause was, until i found the missing return
  • 0
    I've been putting off learning c++ and praying someone would create a better c# for this exact reason.

    c/c++ create a lot of unmanaged errors that sometimes make 0 sense and multiply the more 3rd party stuff you add.
  • 3
    @MadMadMadMrMim that exists and it's called Rust! I love that language 😍
  • 0
    This won't compile if you have appropriate warnings turned on. I've been bitten by this problem exactly 0 times...
  • 0
    @YADU

    -Werror=return-type

    Its "fun" working in 20 year old code bases...
  • 0
    @Demolishun if you're working in a really old codebase, I'd recommend slowly adding warnings and errors for stuff like this to your project.

    Obviously going to take some time but it's better than the alternative
  • 0
    @YADU why does it seem like this is another what's that language called the gnome project never completed.... vala.

    you know that its been long enough you'd think someone would create a decent translator for backending to the gnu compiler suite at the very least implementing common translations for different ways of handling the same problems in different languages and a nice optimizer that says 'oh this is a binary search, oh this is a sort, oh this is an index.... ok so we do that this way in this language' i'd love to see something like that that then backends to c/c++ and compiles straight off at high levels of optimization !
Add Comment