Monday, January 21, 2008

1D arrays basics declaration and memory allocation

Declaring and handling one dimension (1D) arrays

int arr[3] = {1,2,3};

Assuming int = 2 bytes

When declared, compiler will allocate 3, 2 bytes of address to variable arr

arr = 0x1111 [is a memory location] & sizeof(*arr) = 2 bytes; so total arr = 0x1111 to 0x1112
arr + 1 = 0x1113 [is a memory location] & sizeof(*(arr+1)) = 2 bytes; so total arr = 0x1113 to 0x1114
arr + 2 = 0x1115 [is a memory location] & sizeof(*(arr+2)) = 2 bytes; so total arr = 0x1115 to 0x1116

arr[1] = *(arr) = 1 [is a value in 0x1111] - actual value stored in 2 bytes is 0x0001
arr[2] = *(arr+1) = 2 [is a value in 0x1113] - actual value stored in 2 bytes is 0x0002
arr[2] = *(arr+2) = 3 [is a value in 0x1115] - actual value stored in 2 bytes is 0x0003

&arr address of the array of integers, that is entire array itself.
So,

*(&arr) = 0x1111 and sizeof(*(&arr)) = 6 bytes

&arr + 1 = 0x1111 + 6 = 0x1117

1 comment:

Anonymous said...

Bet of the day - online casino and sports betting | Kadangpintar
This is the online 메리트 카지노 casino you want, play kadangpintar and win at. It's also 바카라 home to the best game around: blackjack, baccarat, roulette,