+ 1

# Contiguous memory address allocation in arrays, why does it jump?

I am trying to figure out why two contiguous values of (let's say, a 1D array) have addresses separated by 4 values instead of 1. So for example, if I have int a[3] = {3,4,5}; int *ptr = NULL; ptr = a; Then *ptr will return, for example, 49e4f620 but *ptr + 1 will return 49e4f624. Why is there a difference of 4 instead of 1 in the hexadecimal representation of the address? Sorry ahead if there are errors in my pseudo code, I'm (VERY) new to C :)

5th May 2021, 3:26 PM
Diego Asua Corcostegui
4 ответов
+ 5
It's because you need 4 bytes to store an "int" datatype.
5th May 2021, 3:35 PM
Arsenic
+ 4
The alocated memory depends on the kind of system (ie. 64 or 32 bits) as well as the choosed variable. You can determine this for a vector with sizeof(list[0] and the lengt of it as follows: int list[] = {1,2,3,4,5}; int len = sizeof(list)/sizeof(list[0]);
5th May 2021, 4:10 PM
JaScript
+ 3
Because int reserves 4 bytes of memory and 16^0=1, so you have to multiply it by 4
5th May 2021, 3:36 PM
Michal Doruch
+ 3
The computer architecture stores data in groups of 32 bits(4 bytes), which is also the amount of memory needed to store a number of type integer.
5th May 2021, 3:40 PM
Mihail