Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Are C Arrays Represented In Memory?

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 have an address, and its value will be a different address, which is the space in memory it's pointing to. The following code:

int main(){     int x = 10;     int *Ptr;     Ptr = &x; return 0; } 

Would have the following representation in memory:

+---------------------+-------------+---------+ | 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:

int main(){     int x[5];         x[0]=12;         x[1]=13;         x[2]=14;      printf("%p\n",(void*)x);     printf("%p\n",(void*)&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:

+---------------------+-------------+----------+----------------------+ | Variable Name       | Address     | Value    | Value IF array       | +---------------------+-------------+----------+----------------------+ | x                   | 10568       | 10568    | 12                   | +---------------------+-------------+----------+----------------------+ |                     | 10572       |          | 13                   |  +---------------------+-------------+----------+----------------------+ |                     | 10576       |          | 14                   |  +---------------------+-------------+----------+----------------------+ |                     | 10580       |          | trash                |  +---------------------+-------------+----------+----------------------+ |                     | 10584       |          | trash                |  +---------------------+-------------+----------+----------------------+ 

Is this close to what happens, or completely off?

like image 239
Daniel Scocco Avatar asked Oct 21 '11 01:10

Daniel Scocco


People also ask

How are array in C represented in memory?

Arrays are represented with diagrams that represent their memory use on the computer. These are represented by the square boxes that represent every bit of the memory. We can write the value of the element inside the box.

How do you represent array into memory?

Arrays are often represented with diagrams that represent their memory use. The diagram below is one typical way to represent the memory used by an array. Each box represents the amount of memory needed to hold one array element. For ints this is usually 4 bytes.

How arrays are stored and represented in memory?

An array is just a group of integer, saved in the memory as single integer, but in one row. A integer has 4-Byte in the memory, so you can access each value of your array by increasing your pointer by 4.

How arrays are represented in memory explain with example?

Memory allocation of an array As stated above, all the data elements of an array are stored at contiguous locations in the main memory. The name of the array represents the base address or the address of the first element in the main memory. Each element of the array is represented by proper indexing.


1 Answers

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.

like image 192
caf Avatar answered Sep 28 '22 15:09

caf