sizeof Multi Dimensional array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

sizeof Multi Dimensional array?

like double arr[5][8]; cout << sizeof(arr) << endl; how is that calculated?

26th Jul 2016, 4:08 PM
Jaque
Jaque - avatar
3 Answers
+ 5
it's just multiplication like the times tables from grade school. we can take it apart piece by piece though. let's say we have int arr[5][4]; so sizeof (arr) would be 80. let's go further sizeof (arr [5]) would be 16. this is essentially saying what is the size of the entire fifth column. let's continue sizeof (arr [5][4]) would be 4. and this is where you answer is(well kinda). it's 4 because it's essentially saying what is the size of the value in this specific location. well this is just an array, so it's a collection of variables of a data type. I'm our case it's an int, which is 4 bytes. so when we say what is the sizeof a multi dimensional array we can calculate it by doing (primitive data type size * dimension size 1 * dimension size 2 *...dimension size n-1 * dimension size n).
26th Jul 2016, 4:37 PM
destro
+ 4
it is equal to row*column*data type allocated memory for example data type of int is 2 ex:int arr[5][2]; size=5*2*2 that equals to 20 size of the array allocated is 20
13th Aug 2016, 4:00 PM
Srikanth Dhulipala
Srikanth Dhulipala - avatar
0
sizeof(any_multidimensional_array) = #of_elements * sizeof(arrayDataType) #of_elements = first_dimension_size * second_dimension_size * third_dimen...
29th Dec 2016, 10:39 PM
Nizar Haddad