Reputation: 685
Suppose i have a 3d array declared globably (in data segment) that I want to memset 1d of it to 0.
int multi_dimension_array[x][y][z];
I can memset the whole thing with the line:
memset(multi_dimension_array, 0, sizeof(multi_dimension_array));
But now suppose i only want to memset the x dimension for some value (say 2), meaning the values of multi_dimension_array[2][0][0] to multi_dimension_array[2][y-1][z-1] should all be zero. I dont think theres a clever way to use memset for y or z since they're not contiguous. The following line should work:
memset(&multi_dimension_array[2][0][0], 0, sizeof(multi_dimension_array[2][0][0]) * y * z);
My "issue" is that i dont like the * y * z part of the memset param. Is there something in the array that says sizeof(multi_dimension_array[2]) == byte_size_of_type * y * z?
I want to use a property of the array that sizeof would evaluate to the correct number of bytes for the "x" dimension in this example. I dont want to use * y *z in the event that someone changes the size in the declaration and they do not change this memset, plus i dont like how it looks.
Upvotes: 2
Views: 846
Reputation: 685
For future readers of this question, Eric Postpischil's answer is correct and I have accepted it because it is. Here is a sample program and output to demonstrate the sizeof operator/function as it applies to multidimensional arrays. In general, if declared as an array of ints
int mda[x][y][z];
then mda[a][b][c] == *(mda + ((a *( y* z)) + (b*z) + (c))
Whats important, atleast to the original question i asked, is that indexing into array with fewer "dimensions" than it was declared with your program still will compile and maintain the "sizeof" the next array.
mda[a] == *(mda + (a * (y*z))) AND sizeof(mda[a]) == sizeof(array_type) * y * z
mda[a][b] == *(mda + (a * (y*z)) + (b * z)) AND sizeof(mda[a][b]) == sizeof(array_type) * z
Ok so heres a sample program that you can run in an online IDE and verify:
#include <stdio.h>
#define X (4)
#define Y (10)
#define Z (5)
int multi_dimension_array[X][Y][Z];
void print_array(){
for(int i=0; i<X; i++){
printf("THIS IS THE %dth value of X\n", i);
for(int j=0; j<Y; j++){
for(int k=0; k<Z; k++){
printf("%d ", multi_dimension_array[i][j][k]);
}
printf("\n");
}
}
}
int main(void) {
printf("%d %d %d %d\n", sizeof(multi_dimension_array[0][0][0]), sizeof(multi_dimension_array[0][0]), sizeof(multi_dimension_array[0]), sizeof(multi_dimension_array));
memset(multi_dimension_array,0x01,sizeof(multi_dimension_array));
print_array();
memset(&multi_dimension_array[2],0x0,sizeof(multi_dimension_array[2]));
printf("\n\n\nNEXT MEMSET with the X=2 zeroed out\n\n\n");
print_array();
return 0;
}
And Here is this programs output:
4 20 200 800
THIS IS THE 0th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
THIS IS THE 1th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
THIS IS THE 2th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
THIS IS THE 3th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
NEXT MEMSET with the X=2 zeroed out
THIS IS THE 0th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
THIS IS THE 1th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
THIS IS THE 2th value of X
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
THIS IS THE 3th value of X
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
16843009 16843009 16843009 16843009 16843009
*Note that memset sets each byte to the value of the second paramater, which in this case was 0x01 which makes the 4 byte ints 0x01010101 == 16843009.
Upvotes: 0
Reputation: 222273
memset(&multi_dimension_array[2], 0, sizeof multi_dimension_array[2]);
This requires that multi_dimension_array[i]
be an array of arrays of arrays, not a pointer. It works because, when sizeof
is applied to an array, it returns the size of the array. The array is not automatically converted to a pointer as in most expressions.
Of course, it works only for the first dimension (or first several dimensions, if you do more than one). E.g., you can use one memset with array[i]
or array[i][j]
but not with the middle dimensions such as array[???][j]
.
Upvotes: 6