0

For allocating memory to two dimensional array dynamically, I write something like this

int **ar = (int **)malloc(row*sizeof(int*));
for(i = 0; i < row; i++) 
     ar[i] = (int*)malloc(col*sizeof(int));

I came across this code which does the same but i cannot understand the declaration.

double (*buf)[2] = (double (*)[2])malloc(count*sizeof(double [2]));// Explain this 


printf("%d, %d, %d, %d \n",sizeof(double));
printf(" %d",sizeof(buf[0]));
printf(" %d", sizeof(buf));
//prints 8, 16, 16, 4 when count is 3

The output of first printf() is trivial. Please help me with the next two.

daft300punk
  • 169
  • 3
  • 16

1 Answers1

1
double (*buf)[2] = (double (*)[2])malloc(count*sizeof(double [2]));// Explain this 

This

double (*buf)[2]

defines buf to be a pointer to an array of 2 doubles.

This

(double (*)[2])malloc(count*sizeof(double [2]));

can (and shall) be rewritten as

malloc(count * sizeof(double [2]));

The above line allocates memory with the size of count times "size for an array of 2 doubles".

This

=

assigns the latter to the former.

It all ends up with buf pointing to an array of count * 2 doubles.

Access its elements like this

(*buf)[0][0];

Note that this approach creates a pointer to a "linear" array, that is an array where all elements are store in one continues block of memory.

Whereas the approch you 1st mention in you question creates a "scattered" array that is an array where each row might be located in a seperate block of memory.


This

printf("%d, %d, %d, %d \n",sizeof(double));

provokes undefined behaviour, as from its 1st (format) parameter the printf expects four addtional parameters and is being passed only one.

The size of a double typically is 8.

This

printf(" %d",sizeof(buf[0]));

prints the size of the first element that buf points to. As buf points to an array of 2 doubles, it is expected to print 2 times "size of a double" which 2 * 8 = 16.

This

printf(" %d", sizeof(buf));

prints the size of buf. As buf is defined as a pointer, the size of a pointer on is printed. This typically is 4 for a 32bit implementation and 8 for 64bit implementation.

Note: The value of count does not appear in any of the sizes printed above, not directly, nor indireclty, as In C it is not possible to derive from a pointer how much memory had been allocated to it.

alk
  • 69,737
  • 10
  • 105
  • 255