Thread: How Are C Arrays Represented In Memory?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    33

    How Are C Arrays Represented In Memory?

    I believe I understand how normal variables and pointers are represented in memory.

    For example, it's easy to understand that a pointer Ptr will have one address, and its value will be a different address, which is the space in memory it's pointing to. The following code:


    Code:
    int main(){
    	int x = 10;
    	int *Ptr;
    	Ptr = &x;
    return 0;
    }
    Would have the following representation in memory:

    Code:
    +---------------------+-------------+---------+
    | Variable Name       | Address     | Value   | 
    +---------------------+-------------+---------+
    | x                   | 3342        | 10      |
    +---------------------+-------------+---------+
    | Ptr                 | 5466        | 3342    |
    +---------------------+-------------+---------+
    However I find it difficult to understand how arrays are represented in memory. For example the code:


    Code:
    int main(){
    	int x[5];
            x[0]=12;
            x[1]=13;
            x[1]=14;
    
    	printf("%p\n",x);
    	printf("%p\n",&x);
    
    return 0;
    }
    outputs the same address twice (for the sake of simplicity 10568). Meaning that x==&x. Yet *x (or x[0] in array notation) is equal to 12, *(x+1) (or x[1] in array notation) is equal to 13 and so on. How can this be represented? One way could be this:

    Code:
    +---------------------+-------------+----------+----------------------+
    | Variable Name       | Address     | Value    | Value IF array       |
    +---------------------+-------------+----------+----------------------+
    | x                   | 10568       | 10568    | 12                   |
    +---------------------+-------------+----------+----------------------+
    |                     | 10572       |          | 13                   | 
    +---------------------+-------------+----------+----------------------+
    |                     | 10576       |          | 14                   | 
    +---------------------+-------------+----------+----------------------+
    Is this close to what happens, or completely off?
    Last edited by envec83; 10-20-2011 at 07:14 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    An array will be stored contiguously in memory. In most contexts, an array will be converted to a pointer to its first element. Does your output make sense to you now?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Not too bad...

    Arrays are created as contiguous blocks of memory so your x[i] is right behind x[i - 1].

    Multidimensional arrays such as a[5][5] would be created as "an array of arrays" in contiguous memory... that is each row is together, behind the row before it.

    The spacing you are seeing is the result of sizeof(type)... that is the array's values are spaced out according to the type being stored. On modern systems that spacing is usually 4 bytes for int32, or 8 bytes for int64 values.

    You should also note that, unless you are using malloc() to allocate space on the heap for your arrays, they are being created on the programs "stack" which has a size limit... big arrays should be allocated and freed manually on the heap. ("big" being a matter of judgement).

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    Gotcha. Thanks guys.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by envec83 View Post
    Code:
    int main(){
    	int x[5];
            x[0]=12;
            x[1]=13;
            x[1]=14;
    
    	printf("%p\n",x);
    	printf("%p\n",&x);
    
    return 0;
    }
    outputs the same address twice (for the sake of simplicity 10568). Meaning that x==&x. Yet *x (or x[0] in array notation) is equal to 12, *(x+1) (or x[1] in array notation) is equal to 13 and so on. How can this be represented? One way could be this:

    Code:
    +---------------------+-------------+----------+----------------------+
    | Variable Name       | Address     | Value    | Value IF array       |
    +---------------------+-------------+----------+----------------------+
    | x                   | 10568       | 10568    | 12                   |
    +---------------------+-------------+----------+----------------------+
    |                     | 10572       |          | 13                   | 
    +---------------------+-------------+----------+----------------------+
    |                     | 10576       |          | 14                   | 
    +---------------------+-------------+----------+----------------------+
    Is this close to what happens, or completely off?
    :eyebrow:


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    I get the same address for x and &x also.

    How can the pointer x be stored at 10568 (for example), when x[0] is also stored at 10568?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by megafiddle
    I get the same address for x and &x also.

    How can the pointer x be stored at 10568 (for example), when x[0] is also stored at 10568?
    Read the previous responses
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Yes I did. Still don't see it.

    x is a pointer to address 10568, the 1st element of the array.

    Address 10568 contans a 12.

    x has the value 10568.
    *x has the value 12.

    where is the value of x stored? (the 10568)?

    It would seem that the address value 10568 is also stored at address 10568

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by megafiddle
    x is a pointer to address 10568, the 1st element of the array.

    Address 10568 contans a 12.

    x has the value 10568.
    No, x is not a pointer. x is an array of 5 int elements, hence its value is the value of all the 5 int elements together.

    Quote Originally Posted by megafiddle
    *x has the value 12.
    Yes. At least conceptually, x was converted to a pointer to its first element, and then the result was dereferenced to get the first element, which has a value of 12.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Ok, that makes sense if it's converted.

    But if x is conceptually a pointer to the first element, what does &x mean?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by megafiddle
    But if x is conceptually a pointer to the first element, what does &x mean?
    The address of the array x, which is the same as the address of x's first element, except that the pointer type is different.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    So it's not like a pointer to a pointer, as in the case of passing the address of a pointer
    to a function:

    int *ptr;

    function1( &ptr);

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by megafiddle
    So it's not like a pointer to a pointer, as in the case of passing the address of a pointer
    No, it is like a pointer to a pointer, just like it is like a pointer to an int, or a pointer to a struct, etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    But isn't it pointing to itself?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by megafiddle
    But isn't it pointing to itself?
    Sigh, what points to itself? Compile and run this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x[] = {123, 456};
        int *p = x;
        ++p;
        printf("%d\n", *p);
        return 0;
    }
    Now, try to compile this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x[] = {123, 456};
        ++x;
        printf("%d\n", *x);
        return 0;
    }
    Now, tell me, if x is a pointer, why is it that the second program fails to compile, when what you did with x was exactly the same as what you did with the pointer p?

    EDIT:
    Here's another one to compile and run:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x[] = {123, 456, 789};
        int *p = x;
        printf("%u %u\n", sizeof(x), sizeof(p));
        return 0;
    }
    If both x and p are pointers, why does sizeof(x) differ from sizeof(p)?
    Last edited by laserlight; 10-20-2011 at 10:19 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. How does 0xff get represented on a 16 bit machine?
    By Overworked_PhD in forum C Programming
    Replies: 2
    Last Post: 10-27-2007, 11:32 AM
  3. Array of Booleans Represented with 1 Bit
    By Brad0407 in forum C++ Programming
    Replies: 43
    Last Post: 07-08-2007, 01:23 PM
  4. memory and arrays
    By rwmarsh in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2006, 11:40 AM
  5. Permanent Memory Arrays
    By EricR in forum C++ Programming
    Replies: 6
    Last Post: 04-12-2005, 01:59 PM

Tags for this Thread