Thread: Memory Allocation in Stack

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    18

    Memory Allocation in Stack

    Hello All,
    I have written the following code. I have declared 2 arrays t and s both of size of 4 integers and two integer variables u and i. Please pay attention to the order they have been declared. In the following code, I have printed out the addresses of each array element and variable in the opposite sequence in that they have been declared (s, i, u, t).
    Code:
    # include <stdio.h>
    # include <string.h>
    
    int main()
    {
    	int t[4] = {10, 30, 50, 20}, u = 5;
    	int i;
    	int s[4] = {90, 44, 23, 76};
    
    	for ( i = 0; i < 4; i++ ) {
    		printf ("s[%d] = %u, %d\n", i, s + i, s[i]);
    	}
    
    	printf ("i = %u, %d\n", &i, i);
    
    	printf ("u = %u, %d\n", &u, u);
    	
    	for ( i = 0; i < 4; i++ ) {
    		t[i] = i;
    		printf ("t[%d] = %u, %d\n", i, t + i, t[i]);
    	}
    
    	return 0;
    }
    The output of the program is as follows:
    Code:
    s[0] = 3216459112, 90
    s[1] = 3216459116, 44
    s[2] = 3216459120, 23
    s[3] = 3216459124, 76
    i = 3216459128, 4
    u = 3216459132, 5
    t[0] = 3216459136, 0
    t[1] = 3216459140, 1
    t[2] = 3216459144, 2
    t[3] = 3216459148, 3
    It means they have been allocated memory in this order only(reverse of the order in which they have been declared). Can It change on different compiler or OS?

    Operating System: Linux (Mandriva 2010)
    Compiler: gcc 4.4.1

    Thanks and Best Regards,
    Aakash Johari

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Absolutely.
    There is no guarantee that the declaration order of distinct variables has anything to do with the allocation order, or the size of any padding between distinct variables.

    You can only meaningfully compare addresses between
    - elements of the same array
    - members of the same struct/union
    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
    Jan 2011
    Posts
    18
    Quote Originally Posted by Salem View Post
    Absolutely.
    There is no guarantee that the declaration order of distinct variables has anything to do with the allocation order, or the size of any padding between distinct variables.

    You can only meaningfully compare addresses between
    - elements of the same array
    - members of the same struct/union
    But please go through this link.

    It says that memory allocation goes contiguous even for the statically defined variables.

    How Stuff Works

    Thanks and Best Regards,
    Aakash Johari

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Drop that site. Grab a better tutorial/book like The C Book - Table of Contents,
    The C programming language

    Read the c-iaq


    1.3: If I write the code int i, j; can I assume that (&i + 1) == &j?

    Only sometimes. It's not portable, because in EBCDIC, i and j are not adjacent. [a]
    Last edited by Bayint Naung; 01-20-2011 at 08:02 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Forget that site - read the standard if you want a real answer.
    http://www.open-std.org/jtc1/sc22/wg...69/n869.pdf.gz
    Quote Originally Posted by the_standard
    When two pointers are compared, the result depends on the relative locations in the
    address space of the objects pointed to. If two pointers to object or incomplete types both
    point to the same object, or both point one past the last element of the same array object,
    they compare equal. If the objects pointed to are members of the same aggregate object,
    pointers to structure members declared later compare greater than pointers to members
    declared earlier in the structure, and pointers to array elements with larger subscript
    values compare greater than pointers to elements of the same array with lower subscript
    values. All pointers to members of the same union object compare equal. If the
    expression P points to an element of an array object and the expression Q points to the
    last element of the same array object, the pointer expression Q+1 compares greater than
    P. In all other cases, the behavior is undefined.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by aakashjohari View Post
    But please go through this link.

    It says that memory allocation goes contiguous even for the statically defined variables.

    How Stuff Works

    Thanks and Best Regards,
    Aakash Johari
    I haven't read the site, but I disagree that you shouldn't know that stuff. Just remember that it's not portable and you shouldn't rely on it on C code, that doesn't mean the knowledge can't be useful. I have found it very useful, however rarely. Especially to debug things like buffer overflows.
    Just, whatever yo do: do not rely on it! Ever!

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Within a single integer, its bytes are contiguous. As well, for arrays, structs, their elements are allocated contiguously.

    "When the program runs, the computer reserves space for the variable f somewhere in memory."
    - This is correct. It also means that declaration of multiple variables can end up anywhere in memory.

    Their example shows the compiler chose to allocate the variables in the same order as written in the source code. This is not guaranteed. It may be true for some compilers. The C standard does not define correlation between memory address order and source code variable order.

    Their main point was that accessing an array outside of its bounds will likely access memory contents belonging to other variables since variables all occupy some area of memory.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by aakashjohari View Post
    The output of the program is as follows:
    Code:
    s[0] = 3216459112, 90
    s[1] = 3216459116, 44
    s[2] = 3216459120, 23
    s[3] = 3216459124, 76
    i = 3216459128, 4
    u = 3216459132, 5
    t[0] = 3216459136, 0
    t[1] = 3216459140, 1
    t[2] = 3216459144, 2
    t[3] = 3216459148, 3
    It means they have been allocated memory in this order only(reverse of the order in which they have been declared). Can It change on different compiler or OS?

    And, you would care about any of this exactly why???

    Really... what possible difference can it make where the data is actually stored in memory or what order it's in? You know how to access it through variables in your code, that's all that matters.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Salem View Post
    You can only meaningfully compare addresses between
    - elements of the same array
    - members of the same struct/union
    Members of the same struct/union will always appear in the same order with the same distance between them, but they might not be contiguous since certain types have to be aligned and thus the compiler may insert padding to achieve this, e.g.
    Code:
    #include <stddef.h>
    #include <stdio.h>
    
    struct foo {
        char bar;
        int baz;
    };
    
    int main(void)
    {
        printf("offsetof(struct foo, bar): %d\n", offsetof(struct foo, bar));
        printf("offsetof(struct foo, baz): %d\n", offsetof(struct foo, baz));
    
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    That's correct. In the struct's case, "contiguous" is a slight exaggeration. Its elements are still in the order they appear, as in having ever increasing addresses, but default padding to machine word boundaries may introduce gaps. Unless some #pragma pack(1) or other compiler directive specifically suppresses padding. Then they would be truly contiguous.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack-based allocation
    By eXodus31337 in forum C++ Programming
    Replies: 10
    Last Post: 01-05-2009, 11:03 PM
  2. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM