Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > returning stack variables

Reply
Thread Tools

returning stack variables

 
 
Guray Acar
Guest
Posts: n/a
 
      06-30-2003
Hi,

please consider the code:

int myFunction(void)
{ int x=0;

return x;
}

Now, my friend claimed that the code above is erroneous
because the int variable x is created in the stack. The function
returns the address of x in the stack. But the addresses in the
stack may change outside the function myFunction.

I found his argument quite plausible. However, I have seen
so many functions like the one above. I have written functions
doing what myFunction does, and they worked without error
for long time.

Can anyone clarify this issue ? TIA..

Regards

Guray Acar


 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      06-30-2003
Guray scribbled the following:
> Hi,


> please consider the code:


> int myFunction(void)
> { int x=0;


> return x;
> }


> Now, my friend claimed that the code above is erroneous
> because the int variable x is created in the stack. The function
> returns the address of x in the stack. But the addresses in the
> stack may change outside the function myFunction.


Your friend is talking out of his tailpipe. He's just plain wrong.
The function doesn't return the address of *ANYTHING AT ALL*. It
returns the *VALUE* of x, which is a separate concept from x itself.
Your function is completely OK, legal, valid, kosher, all-singing,
all-dancing and hunky dory.

> I found his argument quite plausible. However, I have seen
> so many functions like the one above. I have written functions
> doing what myFunction does, and they worked without error
> for long time.


His argument would be plausible *IF* you were returning the address
of x. But you aren't, so his argument dissolves like O'Boy cocoa
powder into a glass of cold milk and you win your argument.
*THIS* kind of code would be illegal, because of the reason your
friend gives:

int *myFunction(void) {
int x=0;
return &x;
}

but that's a whole different kettle of fish.

> Can anyone clarify this issue ? TIA..


The issue is that there is no address returning in the function, only
value returning, and returning values of any variables whatsoever is
safe.

--
/-- Joona Palaste () ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
- Douglas Adams
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      06-30-2003
Joona I Palaste <> scribbled the following:
> Guray scribbled the following:
>> Hi,


>> please consider the code:


>> int myFunction(void)
>> { int x=0;


>> return x;
>> }


>> Now, my friend claimed that the code above is erroneous
>> because the int variable x is created in the stack. The function
>> returns the address of x in the stack. But the addresses in the
>> stack may change outside the function myFunction.


> Your friend is talking out of his tailpipe. He's just plain wrong.
> The function doesn't return the address of *ANYTHING AT ALL*. It
> returns the *VALUE* of x, which is a separate concept from x itself.
> Your function is completely OK, legal, valid, kosher, all-singing,
> all-dancing and hunky dory.


>> I found his argument quite plausible. However, I have seen
>> so many functions like the one above. I have written functions
>> doing what myFunction does, and they worked without error
>> for long time.


> His argument would be plausible *IF* you were returning the address
> of x. But you aren't, so his argument dissolves like O'Boy cocoa
> powder into a glass of cold milk and you win your argument.
> *THIS* kind of code would be illegal, because of the reason your
> friend gives:


> int *myFunction(void) {
> int x=0;
> return &x;
> }


> but that's a whole different kettle of fish.


>> Can anyone clarify this issue ? TIA..


> The issue is that there is no address returning in the function, only
> value returning, and returning values of any variables whatsoever is
> safe.


....as long as those values are scalars, addresses of static or global
variables, or addresses passed in to the function, that is. Not just
any old values. This kind of function is still illegal:

int *myFunction(void) {
int x=0;
int *y=&x;
return y;
}

--
/-- Joona Palaste () ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      06-30-2003
In 'comp.lang.c', Guray Acar wrote:

> please consider the code:
>
> int myFunction(void)
> { int x=0;
>
> return x;
> }


Sounds good to me.

> Now, my friend claimed that the code above is erroneous
> because the int variable x is created in the stack. The function


Say, 'in the automatic memory'.

> returns the address of x in the stack. But the addresses in the


No. This function is not returning any address, but a value. It's just fine.

> stack may change outside the function myFunction.


This is misleading. The point is that the address of an automatic variable is
only valid in the block it was defined. Period. Out of the block, no hope.

> I found his argument quite plausible. However, I have seen
> so many functions like the one above. I have written functions
> doing what myFunction does, and they worked without error
> for long time.


/This/ is bad:

int *myFunction(void)
{
int x = 0;

return &x;
}

Don't do that. Never.

--
-ed- [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-30-2003
Guray Acar writes:
[...]
> int myFunction(void)
> { int x=0;
>
> return x;
> }
>
> Now, my friend claimed that the code above is erroneous
> because the int variable x is created in the stack. The function
> returns the address of x in the stack. But the addresses in the
> stack may change outside the function myFunction.


As others have pointed out, your friend is mistaken; there's nothing
wrong with the above code.

But here's something that looks similar, but will cause the kind of
problem your friend warned you about:

char *myFunction(void)
{
char s[] = "hello";
return s;
}

Despite the similar appearance, the return statement here really is
returning the address of s not its value. That's because "s" is
declared as an array, and any reference to the name of an array (other
than as an operand of the sizeof and "&" operators) "decays" to the
address of the array's first element.

(Note that this decay doesn't happen for struct and union types.)

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
 
Reply With Quote
 
Jason Xie
Guest
Posts: n/a
 
      07-01-2003



> But here's something that looks similar, but will cause the kind of
> problem your friend warned you about:
>
> char *myFunction(void)
> {
> char s[] = "hello";
> return s;
> }


I think the above is erroneous too, how about change it as this:
char *myFunction(void)
{
const char s[] = "hello";
return s;
}


--
Jason
The world is a huge family, Love each other.


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      07-01-2003
Jason Xie <> scribbled the following:



>> But here's something that looks similar, but will cause the kind of
>> problem your friend warned you about:
>>
>> char *myFunction(void)
>> {
>> char s[] = "hello";
>> return s;
>> }


> I think the above is erroneous too, how about change it as this:
> char *myFunction(void)
> {
> const char s[] = "hello";
> return s;
> }


This should cause the same problem. This, however, is fine:

char *myFunction(void)
{
char *s = "hello";
return s;
}

as long as you do not try to modify the returned string.

--
/-- Joona Palaste () ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Immanuel Kant but Genghis Khan."
- The Official Graffitist's Handbook
 
Reply With Quote
 
Giuseppe
Guest
Posts: n/a
 
      07-01-2003
On Mon, 30 Jun 2003 21:16:19 GMT, Keith Thompson <> wrote:

>Guray Acar writes:
>[...]
>> int myFunction(void)
>> { int x=0;
>>
>> return x;
>> }
>>
>> Now, my friend claimed that the code above is erroneous
>> because the int variable x is created in the stack. The function
>> returns the address of x in the stack. But the addresses in the
>> stack may change outside the function myFunction.

>
>As others have pointed out, your friend is mistaken; there's nothing
>wrong with the above code.
>
>But here's something that looks similar, but will cause the kind of
>problem your friend warned you about:
>
> char *myFunction(void)
> {
> char s[] = "hello";
> return s;
> }
>
>Despite the similar appearance, the return statement here really is
>returning the address of s not its value. That's because "s" is
>declared as an array, and any reference to the name of an array (other
>than as an operand of the sizeof and "&" operators) "decays" to the
>address of the array's first element.
>
>(Note that this decay doesn't happen for struct and union types.)


char *myFunction(void)
{static char s[] = "hello";
return s;
}

is this ok?
 
Reply With Quote
 
Nick Austin
Guest
Posts: n/a
 
      07-01-2003
On Tue, 01 Jul 2003 09:59:15 GMT, w (Giuseppe)
wrote:

>char *myFunction(void)
>{static char s[] = "hello";
> return s;
>}
>
>is this ok?


Yes.

But this is one instance where you may want to declare the
return type as const char * to avoid someone writing:

myFunction()[0] = 'H';

Nick.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-02-2003
E. Robert Tisdale wrote:

> Guray wrote:
>>
>>
>> please consider the code:
>>
>> int myFunction(void) {
>> int x = 0;
>> return x;
>> }
>>
>> Now, my friend claimed that the code above is erroneous

<snip>
> No. You got it wrong.


How on earth do you know that?

> Your friend claimed that
>
> int& myFunction(void) {
> int x = 0;
> return x;
> }
>
> returns a reference to local variable x
> which no longer exists when the thread of execution
> returns from function myFunction.


The OP's code at least had the merit of being legal C. Your code is not
legal C. No surprise there.


--
Richard Heathfield :
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
Returning "stack" variables from a function Generic Usenet Account C Programming 6 04-27-2005 02:58 PM
"stack level too deep"... because Threads keep their "starting" stack Sam Roberts Ruby 1 02-11-2005 04:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57