简体   繁体   中英

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 the difference between these two memset statements?

Any answer will be highly appreciated. Thanks in advance.

What's the difference between these two memset statements?

The memset function takes, the destination, value and count. The count is sizeof(color) which would be sizeof(int) * 1001 * 1001 for the first call.

For the second it will be sizeof(int) * 10 * 10 .

The former clears the complete array with zeros, while the latter does it only partially, starting from color[0][0] to color[0][99] , which relies on the fact that arrays are laid out in a row-major fashion. Relevant excerpt from the C11 standard (draft n1570), §6.5.2.1 Array subscripting :

[…] It follows from this that arrays are stored in row-major order (last subscript varies fastest).

Alternatively, if m = n = 1001 ie m and n actually denote the array's dimensions, the two calls then are the same, just two different ways of writing it .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM