Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is an array variable stored in C language?

Tags:

arrays

c

pointers

#include <stdio.h>

int main(){
    int array[] = {1,2,3};
    printf("L00: array[0]=%d \n",array[0]);
    printf("L01: array[1]=%d \n",array[1]);
    printf("L02: array[2]=%d \n",array[2]);
    printf("L03: &array[0]=%p \n",&array[0]);
    printf("L04: &array[1]=%p \n",&array[1]);
    printf("L05: &array[2]=%p \n",&array[2]);
    printf("L06: array=%p \n",array);
    printf("L07: &array=%p \n",&array);
    return 0;
}

In this program, L03,L06, and L07 show (&array,array, and &array[0], respectively) the same memory address.

So I'm curious about the location of the "array" as a variable in RAM.

If there is no location of "array" in RAM, where is it?

like image 238
mallea Avatar asked Jun 16 '17 06:06

mallea


4 Answers

The C11 standard specification does not say anything about RAM, IIRC (but check that by yourself, read n1570). On most operating systems, a process has a virtual address space (and your program would run in that space, not necessarily in RAM).

Your array is (likely to be) located on the call stack.

I guess that an hypothetical optimizing compiler might be clever enough to put it elsewhere (but I cannot name such a compiler).

like image 83
Basile Starynkevitch Avatar answered Oct 17 '22 18:10

Basile Starynkevitch


I must have written this many times, but lets do it again:

+----------+----------+----------+
| array[0] | array[1] | array[2] |
+----------+----------+----------+
^          ^          ^          ^
|          |          |          |
array      |          |          |
|          |          |          |
array + 0  array + 1  array + 2  |
|          |          |          |
&array[0]  &array[1]  &array[2]  |
|                                |
&array                           |
|                                |
&array + 0                       &array + 1

As you can see from the above "image" the first element can be reached through five pointers (two really, array decays to &array[0], and array + 0 is equal to array).

The type of these pointers may be different though:

  • &array is of type int (*)[3]
  • &array + 0 is equal to &array and is therefore of the same type
  • &array[0] is of type int *
  • array decays to &array[0] and is therefore of the same type
  • array + 0 is equal to array and is therefore of the same type

There are of course other ways of getting pointers to a specific element, but they are really just variants of the above. For example (&array)[0] is equal to &array + 0 which is equal to &array.

Another thing that is good to know is that for any pointer or array p and index i, the expression p[i] is exactly the same as *(p + i).


The biggest thing you should learn from this is the difference between array and &array. Even though they both point to the same location, their types are different which makes it very different semantically.

like image 35
Some programmer dude Avatar answered Oct 17 '22 17:10

Some programmer dude


The C standard does not mandate where the array is stored, other than it having "automatic storage duration" and local scope. Larger variables with automatic storage duration are almost certainly stored on the stack.

As for why those 3 different forms of syntax give the same address:

  • array, whenever used in an expression, decays into a pointer to the first element. Making it 100% equivalent to &array[0].
  • &array is a pointer to the whole array - an array pointer. This is a different type which can be used to do pointer arithmetic on whole arrays, but apart from that it will still point at the very same address, where the array begins.
like image 6
Lundin Avatar answered Oct 17 '22 17:10

Lundin


(&array,array and &array[0]) the same memory address.

Yes, but they differ in type.

  • &array is of type int (*) [3].
  • array is of type int [3] (Which at times, decay to a pointer to the first element)
  • &array[0] is of type int *.

By definition, the memory allocation for the array is contiguous, it starts at &array[0] and goes up to n-1 elements, n being the dimension.

If there is no location of "array" in RAM, where is it?

If by "RAM" you mean physical memory address, then your concept is wrong. Most of the cases, you are not using any physical memory directly, it's abstracted to you. The memory locations which get printed are from the virtual process address space allocated to your program.

Based on the storage duration, you can see the location varies, i.e., if the array has automatic storage, it'll reside on the function stack (call stack), it it has got static storage, probably it'll be on .bss section. Sometimes it might get optimized out also (not in this case, if left unused), so this depends on the environment. There's no guarantee of allocation unless compiler decided to allocate it.

like image 4
Sourav Ghosh Avatar answered Oct 17 '22 17:10

Sourav Ghosh