Simple C++ question?

Started by
21 comments, last by Zakwayda 14 years, 7 months ago
just in case a compiler doesn't exactly follow standards.
Advertisement
Quote:Original post by Denzin
just in case a compiler doesn't exactly follow standards.
Take that line of reasoning to its logical conclusion, and you will be writing software to run in 32 KB of memory, without OS support, and compiling with Visual C++ 6, "Just in case" [wink]

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:just in case a compiler doesn't exactly follow standards.
Yeah, I'd have to say that's not a very good reason. There may be cases where you have to work around a particular compiler bug or idiosyncrasy, but there's really no need to avoid using basic language features 'just in case' some compiler or other doesn't implement them correctly.
Quote:Original post by Denzin
just in case a compiler doesn't exactly follow standards.
If you mean the compiler might not support an implicit return then it would give you a compile-time error which you can solve by putting in a return 0 just as a one-off fix for that erroneous-error with that particular compiler. This is the usual case of coding around a compiler's idiosyncrasy once it's been identified.

If you mean that a compiler, whilst supporting an implicit return, might not be returning a success code then all bets are off anyway; in that situation there's just as much chance that explicitly returning zero won't signal a successful finish either.

I'm all for defensive coding but in a non-standards compliant world anything can happen and trying to second-guess the nature of crap compilers before you even know how and why they're crap seems arduous.
Quote:Original post by jyk
I'm also curious as to why you think returning zero explicitly at the end of main() is a good habit (I'm not saying it isn't - I'm just curious as to your reasons).


It's absolutely necessary to give the user the possibility to observe wether a program has ended successfully or not. Comes in very handy when you are writing a script which controls running and exiting processes for example.

Quote:Original post by Denzin
just in case a compiler doesn't exactly follow standards.

And just which compilers have you run across that don't follow this rule?

Quote:Original post by lama
Quote:Original post by jyk
I'm also curious as to why you think returning zero explicitly at the end of main() is a good habit (I'm not saying it isn't - I'm just curious as to your reasons).


It's absolutely necessary to give the user the possibility to observe wether a program has ended successfully or not. Comes in very handy when you are writing a script which controls running and exiting processes for example.


And if control reaches the end of main() without encountering a return statement, then compiler will automatically insert the return 0 for you.
Quote:Original post by lama
Quote:Original post by jyk
I'm also curious as to why you think returning zero explicitly at the end of main() is a good habit (I'm not saying it isn't - I'm just curious as to your reasons).
It's absolutely necessary to give the user the possibility to observe wether a program has ended successfully or not. Comes in very handy when you are writing a script which controls running and exiting processes for example.
No, though that would be a common misconception. Standard C++ has no notion of the significance of program return values.

As I mentioned before, the only time it matters is if you are developing for a POSIX certified environment (i.e. Solaris or Mac OS X). And even there, returning zero has no meaning - if you want to signal success or failure to the enclosing environment, you need to return EXIT_SUCCESS or EXIT_FAILURE.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

@swiftcoder:

While there may not be a standard mandating it somewhere, the value from main() has plenty of meaning across Linux and Windows flavours. If you keep your return values in the range 0-255, you are pretty safe on any reasonable OS.

Its a de facto standard, even if it is not written in stone anywhere.
Quote:It's absolutely necessary to give the user the possibility to observe wether a program has ended successfully or not. Comes in very handy when you are writing a script which controls running and exiting processes for example.
The issue under discussion (at least as I understand it) is not 'should I return a value from main()?', but rather, 'if I'm going to return 0 no matter what, should I add the return statement myself, or let the compiler do it for me?'.
Quote:Original post by rip-off
@swiftcoder:
While there may not be a standard mandating it somewhere, the value from main() has plenty of meaning across Linux and Windows flavours. If you keep your return values in the range 0-255, you are pretty safe on any reasonable OS.
Sure, but what does it mean? Blindly returning a value in the range 0-255 doesn't do any good - it doesn't tell anyone anything, since any of those values could indicate any status. In particular, a value in the range 0-255 does *not* indicate success under the POSIX standard, only EXIT_SUCCESS does that (and EXIT_SUCCESS could be set to any value).

At any rate, I maintain only that In the general case, across multiple platforms, anything other than the default return of zero is a waste of time.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement