Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is Stack Smashing Code standard?

11 views
Skip to first unread message

DK

unread,
Jan 12, 2007, 4:33:28 AM1/12/07
to
The following program gives the output as the last initialized variable
in the function func. Is this type of code behaviour standard or just a
gcc quirk?

#include <iostream>
using namespace std;
int func(int);
int main() {
int n = func(2);
cout << n << endl;
return 0;
}
int func(int a) {
int b = a*a;
int c = b*b;
}

Output: 16
Thanks in advance.

jacob navia

unread,
Jan 12, 2007, 6:25:26 AM1/12/07
to
DK a écrit :

This is undefined behavior. You fail to specify the return value.
Anything can happen, including (but not limited to)
returning the last value used.

Paul Pluzhnikov

unread,
Jan 12, 2007, 9:52:18 AM1/12/07
to
jacob navia <ja...@jacob.remcomp.fr> writes:

> DK a écrit :
>> The following program gives the output as the last initialized variable
>> in the function func.

For some platforms and some versions of gcc and some compilation
options:

# Linux/x86
$ g++ -g junk.cc && ./a.out
16
$ g++ -g junk.cc -O && ./a.out
0

# Solaris/SPARC
$ g++ -g junk.cc && ./a.out
16
$ g++ -g junk.cc -O && ./a.out
2

> Anything can happen, including (but not limited to)
> returning the last value used.

And (as above) anything *does* happen ...

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.

0 new messages