Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
C0D4669445ySeems good to me.
I "think" the return 0 is optional.
You could always hit run and see what happens. -
C0D4669445y@stop I'm not a c++ dev, but my limited knowledge is is telling me it's not needed.
#LMGTFY:
On most operating systems returning 0 is a success status like saying "The program worked fine". In C++ it is optional to type "return 0;" at the end of the main function and the compiler includes it automatically -
pk7611705yWhat's the default value for an integer? 0 right? So if you didn't initialize the return "variable" what's going to be there. The default value: 0.
-
@C0D4 It seems to be. Only for the main though. If it's not the main, the compiler gives an error.
@pk76 Actually, an uninitialized value has the content of whatever was in ram before that variable at the same spot.
So it's basically random or better: undefined.
I asked that question, because I forgot to include a return value in an Arduino-IDE and the compiler did random (undefined) stuff in the methods code only when compiled on specific platforms. -
@pk76 GCC compiled programs (only tested for C, not C++) will in general not return 0, if not explicitly written.
-
Define valid. It is in the c++ spec that if you don't explicitly return anything from main(), and the return type is int, that it should return 0. AFAIK all compilers obey this.
HOWEVER! If you turn on all the switches for GCC, (--pedantic -Wall) it should generate a warning. With -Werror that warning will become an error and refuse to compile.
Is it valid C++? Absolutely. But it is a warning. And if you play it super safe like me, it shouldn't compile. -
@pk76 @sbiewald
I just tested it with a simple c program using gcc and linux. The value is always 0.
Possible ideas why:
- Either that's because you start out with default registers of 0.
- The OS will clear any newly reserved space that is given to any program to run to increase security.
- The program got initialized in a still unused area of ram that may be set to zero (idk what whether ram is zeroed or random is after it got trained on post).
But a longer running program would probably still get undefined values since their registers can be anything and you'll probably reuse your own memory at some point.
(Using a loop confirms that.) -
@LinusCDE Fascinating. I was really sure last year's Ubuntu's GCC made something different from it.
-
The compiler allows there to be no return statement in a none-void function, BUT the value the function returns is not defined i. e. some compiler make them return zero and for some others it is practically random (on x86 the current value in eax but that's not relevant to you right now).
-
nekokatt865y@LinusCDE on X86 the register gets xor'ed with itself as default behaviour, so it becomes 0.
That makes sense otherwise you'd have an application that could not deterministically exit successfully -
Funnily enough i think you can even omit the int return type (in some compilers at least)
-
@gitreflog Well I did that accidently in Arduino (variant of gcc).
It didn't like that at all and created broken code. -
@LinusCDE For me i havent found C compilers that rejected it. They usually create warnings so if you have set your build to not allow warnings, that might be the cause. ( https://repl.it/@romangraef/... )
Related Rants
Anyone here knows whether this is valid?
This is the default code for a new Visual Studio C++ Console Project.
rant
default syntax wrong
wtf?
no return for in-function