Multidimensional arrays allocated through calloc
Solution 1
Are the first two ways of defining the array equivalent in memory?
Not quite. In the second type they are almost certainly contiguous, while in the first type this is not sure.
Type 1: in-memory representation will look like this:
+---+---+---+---+---+---+---+---+---+---+
double| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+---+
^
|------------------------------------
. . . . . . . . | // ten rows of doubles
-
+---+---+---+---+---+---+---+---+---+--|+
double| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0||
+---+---+---+---+---+---+---+---+---+--|+
^ . . . -
| ^ ^ ^ . . . . . |
| | | | ^ ^ ^ ^ ^ |
+-|-+-|-+-|-+-|-+-|-+-|-+-|-+-|-+-|-+-|-+
array1[ii]| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | // each cell points to ten doubles
+---+---+---+---+---+---+---+---+---+---+
^
|
|
+-|-+
array1| | |
+---+
Type 2: in-memory representation will look like this:
+---+---+---+---+---+---+---+---+---+---+ +---+
double| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 |
+---+---+---+---+---+---+---+---+---+---+ +---+
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | | | | |
| | | | | | | | | | |
+-|-+-|-+-|-+-|-+-|-+-|-+-|-+-|-+-|-+-|-+ +-|-+
array1[ii]| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ... |99 | // each cell points to one double
+---+---+---+---+---+---+---+---+---+---+ +---+
^
|
|
+-|-+
array1| | |
+---+
Solution 2
Simple Example
#include<stdio.h>
#include<stdlib.h>
int **d ;
int sum();
//----------------------------------------------
int main(){
d = (int **)calloc(3,sizeof(int*));
printf("\n%d",sum());
}
//-----------------------------------------------
int sum(){
int s = 0;
for(int i = 0; i < 3; i++)
d[i] = (int *) calloc (3,sizeof(int));
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
d[i][j] = i+j;
s += d[i][j];
printf("\n array[%d][%d]-> %d",i,j,d[i][j]);
}
}
return s;
}
Kitchi
I'm a physicist-in-training, who likes to dabble in all sorts of stuff.
Updated on July 27, 2022Comments
-
Kitchi almost 2 years
I have a question about how memory is allocated when I
calloc
. I had a look at this question, but it doesn't address how memory is allocated in the case of a dynamically allocated two dimensional array.I was wondering if there was a difference in the memory representation between the following three ways of dynamically allocating a 2D array.
Type 1:
double **array1; int ii; array1 = calloc(10, sizeof(double *)); for(ii = 0; ii < 10; ii++) { array1[ii] = calloc(10, sizeof(double)); } // Then access array elements like array1[ii][jj]
Type 2:
double **array1; int ii; array1 = calloc(10 * 10, sizeof(double *)); // Then access array elements like array1[ii + 10*jj]
Type 3:
double **array1; int ii; array1 = malloc(10 * 10, sizeof(double *)); // Then access array elements like array1[ii + 10*jj]
From what I understand of
calloc
andmalloc
, the difference between the last two is thatcalloc
will zero all the elements of the array, whereasmalloc
will not. But are the first two ways of defining the array equivalent in memory? -
Dancrumb about 11 yearsFirst two ways do not use the same amount of memory. The first method uses ten times the native size of a pointer more.
-
Waterfrag about 11 yearsFirst method : 10 pointers, 100 double, second method : 100 pointers, no double, yeah not same amount my bad. But the second method still has no double allocated.
-
alk about 11 yearsThe cases 2 and 3 use less or equal memory then the first.
-
susundberg about 11 yearsYou are absolutely correct. Thats what i tried to explain on the last paragraph with the numbers, but had 'typo' saying 'second' as it was suppose to be 'first'. I edited the post for this.
-
alk about 11 yearsYou are aware that cases 2 and 3 do not allocate any doubles at all, but only pointers to those, are you?
-
susundberg about 11 yearsWell they both allocate chunk of memory (not double memory or double* memory, its just memory!) , and with 64bit machine sizeof(double*) == sizeof(double) == 8 but on 32bit machine sizeof(double*) == 4, while sizeof(double) == 8. So yes, you are correct, the cases 2&3 on the example code are both wrong, they should allocate memory elements of size double, not double*.