## mingw-users

 [Mingw-users] Function return values in mingw From: Mani - 2011-11-04 13:00:54 ```Hi, I noticed some interesting features.. not sure how to explain.. Program 1: #include using namespace std; int myMax(int n1, int n2) { int m = (n1 > n2? n1 : n2); int m2 = n1; } int main() { cout << myMax(10, 25) << endl; return 0; } Program 1 printed 10 as the answer.. Remember no compilation error (that I expected).. Program 2: with the function I change int m2 = 50; (the second statement) then, I get myMax = 25; the program below: #include using namespace std; int myMax(int n1, int n2) { int m = (n1 > n2? n1 : n2); int m2 = 50; } int main() { cout << myMax(10, 25) << endl; return 0; } Program 2 printed 25 as the answer.. can someone explain why?? best, murali. ```
 Re: [Mingw-users] Function return values in mingw From: JonY - 2011-11-04 13:15:57 Attachments: signature.asc ```On 11/4/2011 21:00, Mani wrote: > Hi, > > I noticed some interesting features.. not sure how to explain.. > > Program 1: > #include > using namespace std; > > int myMax(int n1, int n2) { > int m = (n1 > n2? n1 : n2); > int m2 = n1; > } > > int main() { > > cout << myMax(10, 25) << endl; > return 0; > } > > Program 1 printed 10 as the answer.. Remember no compilation error > (that I expected).. > > Program 2: > with the function I change int m2 = 50; (the second statement) > then, I get myMax = 25; the program below: > > #include > using namespace std; > > int myMax(int n1, int n2) { > int m = (n1 > n2? n1 : n2); > int m2 = 50; > } > > int main() { > > cout << myMax(10, 25) << endl; > return 0; > } > > Program 2 printed 25 as the answer.. > > can someone explain why?? Somebody asked this recently, your code is bad. A missing return statement causes undefined behavior. ```
 Re: [Mingw-users] Function return values in mingw From: LRN - 2011-11-04 13:36:26 ```-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 04.11.2011 17:15, JonY wrote: > On 11/4/2011 21:00, Mani wrote: >> Hi, >> >> I noticed some interesting features.. not sure how to explain.. >> >> Program 1: #include using namespace std; >> >> int myMax(int n1, int n2) { int m = (n1 > n2? n1 : n2); int m2 = >> n1; } >> >> int main() { >> >> cout << myMax(10, 25) << endl; return 0; } >> >> Program 1 printed 10 as the answer.. Remember no compilation >> error (that I expected).. >> >> Program 2: with the function I change int m2 = 50; (the second >> statement) then, I get myMax = 25; the program below: >> >> #include using namespace std; >> >> int myMax(int n1, int n2) { int m = (n1 > n2? n1 : n2); int m2 = >> 50; } >> >> int main() { >> >> cout << myMax(10, 25) << endl; return 0; } >> >> Program 2 printed 25 as the answer.. >> >> can someone explain why?? > > Somebody asked this recently, your code is bad. That was him, actually. > A missing return statement causes undefined behavior. Specifically: 1) gcc does not guarantee that int m = (n1 > n2? n1 : n2); and int m2 = n1; happen in the order of writing, unless they have interdepencency (which they don't), and C doesn't require it to give that guarantee. 2) you don't have a return statement in myMax (why gcc haven't warned you is anybody's guess). 3) my guess is that gcc picks the return value somewhere on the stack, and what's on the stack depends upon stack allocations (m and m2), which is why m2 or m are actually used as return values. 4) my guess is that setting m2 to 50 (a constant) changes the order of allocation, and m is allocated the last, and is returned. Or maybe it's not what is allocated the last, but what is allocated the first? That's why it's undefined behaviour - compiler is free to do anything it deems necessary, for its own reasons (usually - for efficiency). Maybe you should explain, why do you want to use functions without return statements so much? P.S. Actually, you're using C++, not C (why?). But you still need a return statement. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJOs+o5AAoJEOs4Jb6SI2Cw1PsH/3vuJJU8JBCcCsJD7ZpcKzaD 3OHAHFcZNcTwyiPEFGADp+xEiBpN5B1oZ/Qz5lND/704yeoW5a1qYwsBDxsz2WED CGaNoFLFolsXzI5dKkNO7HGpeWXkdk+EqFhCQ9nPcF5CHk+hVeKMsEV9fpgn6woZ ZB7wsJ4h2sk/19tdD5YEFYkbR5TKLgwdkse/UJQPn8D914gMzi6tbZJ4iz7dTzcz sEioh+yPBssV9yaSJAjpxTAaZpe7i86NeEhfH+kORx9oJ1d8zRzSW1rGdCdeCCnE RDrb+Crnsb/8zamh1hgZIdpdbKUxw9Cdks0JSWw0a8Y8qHXd0ws5m9WRblifRkc= =Cyd6 -----END PGP SIGNATURE----- ```
 Re: [Mingw-users] Function return values in mingw From: Mani - 2011-11-04 14:17:36 ```Just to let you know -- I do not write such functions.. But when I see someone saying that their code works and send me a code like this, I now need to explain why their code, though apparently working, is not really correct.. :) best, murali. On Fri, Nov 4, 2011 at 9:35 AM, LRN wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On 04.11.2011 17:15, JonY wrote: >> On 11/4/2011 21:00, Mani wrote: >>> Hi, >>> >>> I noticed some interesting features.. not sure how to explain.. >>> >>> Program 1: #include using namespace std; >>> >>> int myMax(int n1, int n2) { int m = (n1 > n2? n1 : n2); int m2 = >>> n1; } >>> >>> int main() { >>> >>> cout << myMax(10, 25) << endl; return 0; } >>> >>> Program 1 printed 10 as the answer.. Remember no compilation >>> error (that I expected).. >>> >>> Program 2: with the function I change int m2 = 50; (the second >>> statement) then, I get myMax = 25; the program below: >>> >>> #include using namespace std; >>> >>> int myMax(int n1, int n2) { int m = (n1 > n2? n1 : n2); int m2 = >>> 50; } >>> >>> int main() { >>> >>> cout << myMax(10, 25) << endl; return 0; } >>> >>> Program 2 printed 25 as the answer.. >>> >>> can someone explain why?? >> >> Somebody asked this recently, your code is bad. > That was him, actually. >> A missing return statement causes undefined behavior. > > Specifically: > 1) gcc does not guarantee that > int m = (n1 > n2? n1 : n2); > and > int m2 = n1; > happen in the order of writing, unless they have interdepencency > (which they don't), and C doesn't require it to give that guarantee. > 2) you don't have a return statement in myMax (why gcc haven't warned > you is anybody's guess). > 3) my guess is that gcc picks the return value somewhere on the stack, > and what's on the stack depends upon stack allocations (m and m2), > which is why m2 or m are actually used as return values. > 4) my guess is that setting m2 to 50 (a constant) changes the order of > allocation, and m is allocated the last, and is returned. > Or maybe it's not what is allocated the last, but what is allocated > the first? > That's why it's undefined behaviour - compiler is free to do anything > it deems necessary, for its own reasons (usually - for efficiency). > > Maybe you should explain, why do you want to use functions without > return statements so much? > > P.S. Actually, you're using C++, not C (why?). But you still need a > return statement. > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.11 (MingW32) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ > > iQEcBAEBAgAGBQJOs+o5AAoJEOs4Jb6SI2Cw1PsH/3vuJJU8JBCcCsJD7ZpcKzaD > 3OHAHFcZNcTwyiPEFGADp+xEiBpN5B1oZ/Qz5lND/704yeoW5a1qYwsBDxsz2WED > CGaNoFLFolsXzI5dKkNO7HGpeWXkdk+EqFhCQ9nPcF5CHk+hVeKMsEV9fpgn6woZ > ZB7wsJ4h2sk/19tdD5YEFYkbR5TKLgwdkse/UJQPn8D914gMzi6tbZJ4iz7dTzcz > sEioh+yPBssV9yaSJAjpxTAaZpe7i86NeEhfH+kORx9oJ1d8zRzSW1rGdCdeCCnE > RDrb+Crnsb/8zamh1hgZIdpdbKUxw9Cdks0JSWw0a8Y8qHXd0ws5m9WRblifRkc= > =Cyd6 > -----END PGP SIGNATURE----- > > ------------------------------------------------------------------------------ > RSA(R) Conference 2012 > Save \$700 by Nov 18 > Register now > http://p.sf.net/sfu/rsa-sfdev2dev1 > _______________________________________________ > MinGW-users mailing list > MinGW-users@... > > This list observes the Etiquette found at > http://www.mingw.org/Mailing_Lists. > We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated. > > _______________________________________________ > You may change your MinGW Account Options or unsubscribe at: > https://lists.sourceforge.net/lists/listinfo/mingw-users > Also: mailto:mingw-users-request@...?subject=unsubscribe > ```
 Re: [Mingw-users] Function return values in mingw From: Keith Marshall - 2011-11-04 21:30:50 ```On 04/11/11 13:35, LRN wrote: > 3) my guess is that gcc picks the return value somewhere on the stack, > and what's on the stack depends upon stack allocations (m and m2), > which is why m2 or m are actually used as return values. Actually, in the case of a function returning an int, (as in the OP's example), the result is returned in the EAX register; when the return statement is executed, it ensures that the appropriate value is moved into EAX, before the function returns. When the return statement is omitted, (as in the OP's example), then whatever arbitrary value remains in EAX, (from the last operation to have used it), becomes the return value. It is possible that this arbitrary value may be (accidentally) equal to the expected result, but is more likely that it will not be. If you omit the return statement, then the behaviour is undefined; inducing undefined behaviour is *always* a programming error. -- Regards, Keith. ```
 Re: [Mingw-users] Function return values in mingw From: Allann Jones - 2011-11-05 04:47:37 Attachments: Message as HTML ```On Fri, Nov 4, 2011 at 5:30 PM, Keith Marshall < keithmarshall@...> wrote: > On 04/11/11 13:35, LRN wrote: > > 3) my guess is that gcc picks the return value somewhere on the stack, > > and what's on the stack depends upon stack allocations (m and m2), > > which is why m2 or m are actually used as return values. > > Actually, in the case of a function returning an int, (as in the OP's > example), the result is returned in the EAX register; when the return > statement is executed, it ensures that the appropriate value is moved > into EAX, before the function returns. > > When the return statement is omitted, (as in the OP's example), then > whatever arbitrary value remains in EAX, (from the last operation to > have used it), becomes the return value. It is possible that this > arbitrary value may be (accidentally) equal to the expected result, > but is more likely that it will not be. If you omit the return > statement, then the behaviour is undefined; inducing undefined behaviour > is *always* a programming error. > > I think that the confusion was the return of the literal resulted from the last instruction on the body of the function. This is the expected behaviour on Perl language but maybe not a standardized behaviour for C / C++. Best regards. ```
 Re: [Mingw-users] Function return values in mingw From: Tor Lillqvist - 2011-11-05 08:19:22 ```> but maybe not a standardized behaviour for C / C++. "maybe"? --tml ```
 Re: [Mingw-users] Function return values in mingw From: Kai Tietz - 2011-11-05 08:34:31 ```2011/11/5 Tor Lillqvist : >> but maybe not a standardized behaviour for C / C++. > > "maybe"? > > --tml For sure not "maybe". It is simply an undefined behavior. The result of a function with return-type, which uses a return statement without expression, is for IA architectures the random value in %eax/%rax register. By chance it might contain expected value, but for sure this isn't something you can trust in. The only scenario a return statement without expression in function with return-type might occure is in combination with inline-assember function, where explicit the %eax/%rax register is set as final statement. But nevertheless this is piggy and no really reliable approach, as you don't know what compiler actually does in function's epilogue. Kai ```