Can anyone tell me what the following statements do?
float (tab)[MAXCLASS];
tab=(float ()[MAXCLASS]) calloc(MAXCLASS,
(MAXCLASS+1)*sizeof(float));
/* creates a pointer named tab that points to an array of MAXCLASS items of type float */
float (*tab)[MAXCLASS];
/*
allocates memory for MAXCLASS elements each of size (MAXCLASS+1)*sizeof(float) bytes and
initializes all memory locations to zeros and casts the pointer to the appropriate type
*/
tab=(float (*)[MAXCLASS]) calloc(MAXCLASS, (MAXCLASS+1)*sizeof(float));
The last one allocates too much memory and IMO the calloc should be...
/*
allocates memory for MAXCLASS elements each of sizeof(float) bytes and sets
the memory locations to zeros and casts the pointer to the appropriate type
*/
tab=(float (*)[MAXCLASS]) calloc(MAXCLASS, sizeof(float));
Forgot to add that though the two statements are equivalent their difference is that this one allocates memory on the stack...
float (*tab)[MAXCLASS];
while the one below allocates memory from the heap...
tab=(float (*)[MAXCLASS]) calloc(MAXCLASS, (MAXCLASS+1)*sizeof(float));
and written as is the heap one uses more memory than the stack one unless written as shown in my previous post.
Thanks very much.