c++ - What's the difference between these two memset? -
int color[1001][1001]; int m,n; m=10; n=10; memset(color,0,sizeof(color)); memset(color,0,sizeof(color[0][0])*m*n );
what's difference between these 2 memset statements?
any answer highly appreciated. in advance.
what's difference between these 2 memset statements?
the memset
function takes, destination, value , count. count sizeof(color)
sizeof(int) * 1001 * 1001
first call.
for second sizeof(int) * 10 * 10
.
the former clears complete array zeros, while latter partially, starting color[0][0]
color[0][99]
, relies on fact arrays laid out in row-major fashion. relevant excerpt c11 standard (draft n1570), §6.5.2.1 array subscripting:
[…] follows arrays stored in row-major order (last subscript varies fastest).
alternatively, if m = n = 1001
i.e. m
, n
denote array's dimensions, 2 calls same, just 2 different ways of writing it.
Comments
Post a Comment