Advertisement

return 0 differ on Linux/Windows

Started by March 14, 2017 03:11 PM
5 comments, last by djsteffey 7 years, 8 months ago

Hi

If I do

return 0;

on windows it immediately terminates the current function but on Linux it seems to only affect at the function end? Well it continues unlike Windows. How to immediately exit the function also on Linux?

Many thanks

Using "return" or "return X" (where X is some value, if the function has a return value) will exit the function in all standards-compliant C++ compilers. This will cause the destructors of objects in scope to execute. It's very likely you're misunderstanding the behavior you're seeing, as it's extremely unlikely this is some kind of Linux/Windows behavioral delta on its own.

Please post the code in question.

Advertisement
On all c-style languages, the return statement exits the current function scope.

You may mistake the console lifetime for the program lifetime.

Niko Suni

So return 0; should exit my while loop? It does on Windows. I'm playing around with break and return false: now without success. So there must be something else wrong with my code?

Thanks

int inith(string, string){

while(getline(nfs, Mtmp)){

if (Mtmp == "my"){
...do stuff...
break;
//return 0; //works on Windows
}

So there must be something else wrong with my code?

int inith(string, string){
while(getline(nfs, Mtmp)){

if (Mtmp == "my"){
...do stuff...
break;
//return 0; //works on Windows
}


Sure, the problem is that it doesn't compile.

Post your *actual* code.

Make sure getline() is not blocked waiting for new data.

Advertisement

uncomment the return statement?

or you are not being consistent in your testing and it is hitting the break; statement and not making it to the return statement in some of your tests.

This topic is closed to new replies.

Advertisement