CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2010
    Posts
    4

    Static array allocation problem

    Consider the following code:

    int a = 5;
    cout << a << " " << &a;

    Obviously "a" prints out 5 and &a prints out the address of a. Now this code:

    int * b = new int[2];
    cout << b << " " << &b;

    I get 2 different memory addresses as expected. However, if I do this:

    int c[] = {5,6,7};
    cout << c << " " << &c;

    I get the exact same memory location. Why are c and &c the exact same address? I'm assuming it has something to do with the static allocation of c and the dynamic allocation of b. Any ideas?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Static array allocation problem

    You can think of C as a pointer to a block of memory which starts at itself. That's just the way stack arrays work.

    It *is* definitely a fun little curiosity. Would probably make a good trivial question.

  3. #3
    Join Date
    Feb 2010
    Posts
    4

    Re: Static array allocation problem

    Quote Originally Posted by Lindley View Post
    You can think of C as a pointer to a block of memory which starts at itself. That's just the way stack arrays work.

    It *is* definitely a fun little curiosity. Would probably make a good trivial question.
    I like the answer but where is "c" stored (is it even stored anywhere)? I mean, it has to be stored in memory somewhere, right? This kind of goes back to my example of "a" where cout << a; prints out the value of a but cout << &a; prints off the address of a. I'm having a hard time seeing why "c" is it's own address. Is there any technical page you have off the top of your head that I can look at. I've scoured the gnu documentation but haven't found what I am looking for.

  4. #4
    Join Date
    Aug 2006
    Posts
    157

    Re: Static array allocation problem

    As Lindley says, that's the way arrays on the stack work. When you write:
    Code:
    int myarray[] = {5, 6, 7};
    You are creating a memory block three ints long. The address of that block is myarray[0] because that's where the memory block starts. It just so happens that C++ let's you refer to that memory location as myarray, without specifying the index.

  5. #5
    Join Date
    Feb 2010
    Posts
    4

    Re: Static array allocation problem

    Quote Originally Posted by sockman View Post
    As Lindley says, that's the way arrays on the stack work. When you write:
    Code:
    int myarray[] = {5, 6, 7};
    You are creating a memory block three ints long. The address of that block is myarray[0] because that's where the memory block starts. It just so happens that C++ let's you refer to that memory location as myarray, without specifying the index.
    The address of that block is myarray, not myarray[0]. myarray[0] would be 5, yes? The address of that block of memory is myarray so what exactly is &myarray... is it also the address of that block because that's what the code is printing out.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Static array allocation problem

    Quote Originally Posted by mcclane400 View Post
    I like the answer but where is "c" stored (is it even stored anywhere)? I mean, it has to be stored in memory somewhere, right?
    Actually, no. In the immediate scope of the array, every reference to "c" can be replaced by the compiler to a constant offset from the stack frame, since c is a local variable.

    If you were to pass c to some other function, then it would "decay" to a pointer holding &c[0], and that decayed pointer would need to be stored somewhere (typically as a function parameter, but it could be stored in any old pointer). But if you don't do that, then there's no need to provide memory to hold the address of the array, because the address of the array is implicit in the stack frame.

    If you need a specific example, consider what the difference is between these two locals:
    Code:
    void func()
    {
        int i;
        int iarr[1];
    }
    There's really no difference except for datatype and syntax. They both reserve space on the stack for exactly one int, and provide a name to refer to that memory by.

  7. #7
    Join Date
    Feb 2010
    Posts
    4

    Re: Static array allocation problem

    Works for me, thanks for the help.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured