How Are C Arrays Represented In Memory?

后端 未结 8 688
醉话见心
醉话见心 2020-12-14 01:33

I believe I understand how normal variables and pointers are represented in memory if you are using C.

For example, it\'s easy to understand that a pointer Ptr will

相关标签:
8条回答
  • 2020-12-14 01:55

    Yes, you've got it. A C array finds the indexed value x[y] by calculating x + (y * sizeof(type)). x is the starting address of the array. y * sizeof(type) is an offset from that. x[0] produces the same address as x.

    Multidimensional arrays are similarly done, so int x[y][z] is going to consume sizeof(int) * y * z memory.

    Because of this you can do some stupid C pointer tricks. It also means getting the size of an array is (almost) impossible.

    0 讨论(0)
  • 2020-12-14 01:57

    An array is a block of contiguous objects with no spaces in between. This means that x in your second example is represented in memory as:

    +---------------------+-------------+---------+
    | Variable Name       | Address     | Value   | 
    +---------------------+-------------+---------+
    | x                   | 10568       | 12      |
    |                     |             +---------+
    |                     |             | 13      |
    |                     |             +---------+
    |                     |             | 14      |
    |                     |             +---------+
    |                     |             | ??      |
    |                     |             +---------+
    |                     |             | ??      |
    +---------------------+-------------+---------+
    

    That is, x is five ints big, and has a single address.

    The weird part about arrays isn't in how they're stored - it's how they're evaluated in expressions. If you use an array name somewhere that it isn't the subject of the unary & or sizeof operators, it evaluates to the address of its first member.

    That is, if you just write x, you will get a value 10568 with type int *.

    If, on the other hand you write &x, then the special rule doesn't apply - so the & operator works like it normally does, which means that it fetches the address of the array. In the example, this will be a value 10568 with type int (*)[5].

    The reason that x == &x is that the address of the first member of an array is necessarily equal to the address of the array itself, since an array starts with its first member.

    0 讨论(0)
  • 2020-12-14 02:00

    The Arrays and Pointers section in the C FAQ has some helpful information.

    0 讨论(0)
  • 2020-12-14 02:10

    An array in C is a sequential block of memory with each member's block of the same size. This is why pointers work, you seek an offset based on the first member's address.

    0 讨论(0)
  • 2020-12-14 02:10

    A C array is just a block of memory that has sequential values of the same size. When you call malloc(), it is just granting you a block of memory. foo[5] is the same as *(foo + 5).

    Example - foo.c:

    #include <stdio.h>
    
    int main(void)
    {
        int foo[5];
        printf("&foo[0]: %tx\n", &foo[0]);
        printf("foo: %tx\n\n", foo);
        printf("&foo[3]: %tx\n", &foo[3]);
        printf("foo: %tx\n", foo + 3);
    }
    

    Output:

    $ ./foo
    &foo[0]: 5fbff5a4
    foo: 5fbff5a4
    
    &foo[3]: 5fbff5b0
    foo: 5fbff5b0
    
    0 讨论(0)
  • 2020-12-14 02:12

    Daniel,

    this is not difficult. You have the basic idea and there's no much difference in memory representation of arrays. if you declare an array, say

         void main(){
             int arr[5]={0,1,2,3,4};
    
    
         }
    

    you have initialized(defined) the array. So the five elements will be stored in five adjacent locations in memory. you can observe this by referencing the memory address of each element. Not like other primitive data types in C, an array identifier(here, arr) itself represents its pointer. The idea seems vague if you are a beginner but you will feel comfortable as you go on.

          printf("%d",arr);
    

    this line will show you the memory address of the first element, arr[0]. This is similar to referencing the address of the first element.

          printf("%d",&arr[0]);
    

    now, you can view memory locations of all elements. The following piece of code will do the job.

        int i;
        for(i=0;i<5;i++){
           printf("location of %d is %d\n",arr[i],&arr[i]);
        } 
    

    you will see each address increments by gaps of four.(if your integers are 32 bits long). So you can easily understand how the arrays are stored in the memory.

    you can also try the same thing using a different method.

        int i;
        for(i=0;i<5;i++){
           printf("location of %d is %d\n",*(a+i),a+i);
        }
    

    you will get the same set of answers in both cases and try to get the equivalence.

    try the same experiment using different data types(char, float and struct types). You will see how the gaps between adjacent elements vary based on the size of a single element.

    0 讨论(0)
提交回复
热议问题