简体   繁体   English

这两个memset有什么区别?

[英]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?这两个 memset 语句有什么区别?

Any answer will be highly appreciated.任何答案将不胜感激。 Thanks in advance.提前致谢。

What's the difference between these two memset statements?这两个 memset 语句有什么区别?

The memset function takes, the destination, value and count. memset函数接受目标、值和计数。 The count is sizeof(color) which would be sizeof(int) * 1001 * 1001 for the first call.计数为sizeof(color) ,第一次调用时为sizeof(int) * 1001 * 1001

For the second it will be sizeof(int) * 10 * 10 .第二个是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.前者用零清除整个数组,而后者只部分清除,从color[0][0]color[0][99] ,这依赖于数组以行为主的事实时尚。 Relevant excerpt from the C11 standard (draft n1570), §6.5.2.1 Array subscripting : C11 标准(n1570 草案)第6.5.2.1 节数组下标的相关摘录:

[…] 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 .或者,如果m = n = 1001mn实际上表示数组的维度,那么这两个调用是相同的,只是两种不同的写法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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