Thread: Local variable is popped from the stack, when the function is done, isn't it?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52

    Local variable is popped from the stack, when the function is done, isn't it?

    As far as I know that local variables are popped from the stack when its scope is done. But in the following code, this information isn't provided. The code is running properly, although local variable is popped from the stack.

    Here are the code:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    struct ComplexNumber{
        int realNo;
        int imaginaryNo;
    };
    
    
    ComplexNumber operator +(ComplexNumber &cN1,ComplexNumber &cN2)
    {
        ComplexNumber temp; // LOCAL VARIABLE
    
    
        temp.realNo = cN1.realNo + cN2.realNo;
        temp.imaginaryNo = cN1.imaginaryNo + cN2.imaginaryNo;
    
    
        return temp;                // LOCAL VARIABLE IS RETURN ?
    }
    
    
    int main()
    {
        ComplexNumber c1;
        ComplexNumber c2;
        ComplexNumber result;
    
    
        c1.realNo = 1;
        c1.imaginaryNo = 2;
    
    
        c2.realNo = 3;
        c2.imaginaryNo = 4;
    
    
        result = c1 + c2;    // There is not any problem, why?
    
    
        cout << "result.realNo = " << result.realNo << endl;
        cout << "result.imaginaryNo = " << result.imaginaryNo << endl;
    
    
        return 0;
    }
    result = c1 + c2; // There is not any problem, why?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > return temp; // LOCAL VARIABLE IS RETURN ?
    A copy of it is returned.

    Consider a simple function like
    Code:
    int foo ( ) {
        return 42;
    }
    Where does the 42 go?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    42 assigns to function's name foo.
    I guess I understand.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > 42 assigns to function's name foo.
    No.

    When you say
    int result = foo();

    It's really the same as
    int result = 42;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think you can compile using gcc with -S flag and it'll spit out the assembly language of your executable so you can check it yourself using that, can't you?

  6. #6
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    Well, I understand. But how can you explain the following codes?

    Code:1
    Code:
    int *foo(int a,int b)
    {
       int x;
    
       x = a + b;
    
       return &x;
    }
    In this code, a return value is expected as adress, but my note says that this line is invalid,because local variable is popped from the stack.

    If I think like you, the return value &x is copied. So even if the local variable is popped from the stack, the copy adress is returned.

    Code2:
    Code:
    int &foo(int a,int b)
    {
       int x;
    
       x = a + b;
       
       return x;
    }
    My note says that this code is invalid, because x is popped. Okay, if you think like you, I can understand this code. But above...?(Code 1)

    Quote Originally Posted by MutantJohn View Post
    I think you can compile using gcc with -S flag and it'll spit out the assembly language of your executable so you can check it yourself using that, can't you?
    I use Code::Blocks, and I don't understand what you want to tell me with -S flag.
    Last edited by hefese; 11-20-2013 at 03:23 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In snippet 1, the value returned is the address of the local variable x. This is still a "real" address, but the variable x doesn't live there any more (since it was discarded when the function ended).

    In snippet 2, the value returned is a reference to the local variable x. (Note the difference with your original post, which returned a ComplexNumber, not a ComplexNumber&.) Again, the local variable doesn't live there any more (or any where), so you have a dangling reference.

    This machine has a slightly old version of CodeBlocks, but I think you still have Settings->Compiler and debugger settings.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    both snippets will produce compiler warnings with any reputable compiler, letting you know that you shouldn't do that.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You can think of the value returned as a variable. A function call will allocate space for the result, call the function, then expect the result in that variable. In the called function, calling return will copy the return expression result into the return slot before jumping back to the original function. This is exactly how it's implemented, with the cravat that the return value can be either a register or a spot on the stack depending on its size and structure.

    So referring to a local variable is not allowed, but making a copy of a local variable in the return register is perfectly fine.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    Thank you for your answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-03-2012, 09:58 PM
  2. warning: function returns addresss of local variable
    By gunitinug in forum C Programming
    Replies: 5
    Last Post: 12-25-2011, 05:39 PM
  3. Local variables popped from stack or is it ?
    By trish in forum C Programming
    Replies: 7
    Last Post: 07-04-2011, 01:42 PM
  4. Error - function returns address of local variable
    By tltying in forum C Programming
    Replies: 5
    Last Post: 05-28-2007, 02:26 AM
  5. Replies: 20
    Last Post: 11-12-2005, 03:10 PM

Tags for this Thread