returning stack variables

Discussion in 'C Programming' started by Guray Acar, Jun 30, 2003.

  1. Guray Acar

    Guray Acar Guest

    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
    Guray Acar, Jun 30, 2003
    #1
    1. Advertising

  2. 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
    Joona I Palaste, Jun 30, 2003
    #2
    1. Advertising

  3. 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
    Joona I Palaste, Jun 30, 2003
    #3
  4. 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/
    Emmanuel Delahaye, Jun 30, 2003
    #4
  5. 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"
    Keith Thompson, Jun 30, 2003
    #5
  6. Guray Acar

    Jason Xie Guest


    > 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.
    Jason Xie, Jul 1, 2003
    #6
  7. 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
    Joona I Palaste, Jul 1, 2003
    #7
  8. Guray Acar

    Giuseppe Guest

    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?
    Giuseppe, Jul 1, 2003
    #8
  9. Guray Acar

    Nick Austin Guest

    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.
    Nick Austin, Jul 1, 2003
    #9
  10. 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
    Richard Heathfield, Jul 2, 2003
    #10
  11. Richard Heathfield wrote:

    > E. Robert Tisdale wrote:
    >
    >>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.


    No.
    But it *is* perfectly legal C++.
    Guray posted to the wrong newsgroup as well.
    E. Robert Tisdale, Jul 2, 2003
    #11
  12. E. Robert Tisdale wrote:

    > Richard Heathfield wrote:
    >
    >> E. Robert Tisdale wrote:
    >>
    >>>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.

    >
    > No.
    > But it *is* perfectly legal C++.


    This is comp.lang.c, not comp.lang.c++.

    > Guray posted to the wrong newsgroup as well.


    He posted C code to a C newsgroup, and asked a topical question about that
    code. You, on the other hand, posted C++ code to a C newsgroup.

    It seems to me that it's /you/ who posted to the wrong newsgroup.

    --
    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
    Richard Heathfield, Jul 2, 2003
    #12
  13. In article <> writes:
    > Richard Heathfield wrote:
    >
    > > [Guray] posted C code to a C newsgroup
    > > and asked a topical question about that code.

    >
    > Guray posted C++ code to the comp.lang.c newsgroup
    > which was similar to C++ code that has the problem
    > which Guray's friend was explaining to him.


    This was the original code:
    > int myFunction(void)
    > { int x=0;
    >
    > return x;
    > }


    what part of it is not C?
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Jul 2, 2003
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Generic Usenet Account

    Returning "stack" variables from a function

    Generic Usenet Account, Apr 27, 2005, in forum: C Programming
    Replies:
    6
    Views:
    424
    Dr A. N. Walker
    Apr 27, 2005
  2. Surinder Singh
    Replies:
    1
    Views:
    1,188
    Richard Bos
    Dec 20, 2007
  3. Casey Hawthorne
    Replies:
    3
    Views:
    1,079
    Flash Gordon
    Nov 1, 2009
  4. Debajit Adhikary
    Replies:
    36
    Views:
    2,281
    Andre Kaufmann
    Feb 10, 2011
  5. Sam Roberts
    Replies:
    1
    Views:
    222
    Yukihiro Matsumoto
    Feb 11, 2005
Loading...

Share This Page