Thread: How are variables stored in memory.

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    40

    How are variables stored in memory.

    Ok so i'm just trying to clear up how different variables are stored in memory/on the stack.

    Say there is a program with global variables, a function that has varibale declared within it, and a main function that has variables declared in it and a call of the other function.

    What I want to know is in what order these variables will be allocated and deallocated.

    I imagine it's like this:

    Global variables are allocated to memory
    Variable within main function allocated to memory (assuming declaration is at the start of main, before the other function is called)
    Function is now called, and variable within this are allocated to memory. Return value allocated to memory.
    Variables within function deallocated
    Return falue from function deallocated
    Return value of main allocated to memory
    Variables of main deallocated
    Global variables deallocated.

    Apologies if my terminology is incorrect. Would this seem the correct order for variables to be pushed/pulled to and from the stack? Or should variables from the functions be allocated when the function is first declared? Or am I correct in thinking they should only be allocated when the function is called in the main function?

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    54
    This may make things a bit more clear for you:

    http://en.wikipedia.org/wiki/Stack_frame#Structure

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You are basically correct -- local variables are created when the function is called. It must be so, otherwise recursion would be impossible.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Return values from a purely assembly language standpoint don't really need memory allocated unless they are being stored in another variable. Integral return values in C/C++ are stored in EAX and floating point return values are stored in ST(0). If there is no further need for these variables a simple cmp instruction can be executed and then flow can branch based on the results. No memory allocation is required for this type of code.

    For instance:

    Code:
    int function();
    ...
    ...
    if (function())
    {
        Foo();
    }
    Since function returns int the result of the function will be stored in EAX by the compiler. The result of function() could be placed into a temporary variable which would need memory or it could also be placed in another register in preparation for the eventual cmp that will be executed. I suspect that most compilers can optimize this type of code if the result of function() is never used again meaning that no memory needs to be allocated to both store and examine the return value of function(). It really depends on the state of the program at the time and what registers are being used for which purposes. Compilers are extremely smart, though, so I suspect that if the result of function() is not needed later in the code...any good compiler would optimize this out to a simple register comparison.
    Last edited by VirtualAce; 04-29-2010 at 05:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can this be done - multiple variables stored
    By spadez in forum C Programming
    Replies: 2
    Last Post: 04-14-2009, 10:13 AM
  2. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. Variable Storing in memory
    By karb0noxyde in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 07:31 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM