- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在阅读有关数组/结构默认初始化值的内容,并有以下问题:
memset(&mystruct, 0, sizeof mystruct)
是否与 mystruct = { 0 };
相同?
如果不是,有什么区别?
最佳答案
is memset(&mystruct, 0, sizeof mystruct) same as mystruct = { 0 }; ?
没有。
memset(&mystruct, 0, sizeof mystruct) ;
... 将告诉编译器调用一个函数,我们希望该函数在执行期间将 mystruct 中的数据设置为零。
mystruct = { 0 };
...将设置告诉编译器自己将数据设置为零,这意味着它将:
请注意,也许编译器可以将 memset 优化为编译时指令(例如用第二个版本替换第一个版本),但我不会依赖因为 memset
是运行时库中的一个函数,而不是某种语言固有的(虽然我不是编译器编写者/语言律师)。
来自 C++,我自己的观点是,你在编译时可以做的越多,编译器在编译时知道的越多,甚至在执行开始之前,就越好:它使编译器有可能优化代码和/或生成警告/错误。
在当前情况下,使用 mystruct = { 0 };
符号来初始化 struct
总是比使用 memset 更安全,因为它非常非常 使用 memset
很容易在 C 语言中写错东西,编译器不会报错。
以下示例表明代码很容易做一些与看起来不同的事情:
// only the 1st byte will be set to 0
memset(&mystruct, 0, sizeof(char)) ;
// will probably overrun the data, possibly corrupting
// the data around it, and you hope, crashing the process.
memset(&mystruct, 0, sizeof(myLARGEstruct)) ;
// will NOT set the data to 257. Instead it will truncate the
// integer and set each byte to 1
memset(&mystruct, 257, sizeof(mystruct)) ;
// will set each byte to the value of sizeof(mystruct) modulo 256
memset(&mystruct, sizeof(mystruct), 0) ;
// will work. Always.
mystruct = { 0 } ;
关于c - memset(&mystruct, 0, sizeof mystruct) 和 mystruct = { 0 }; 一样吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10788861/
这个问题已经有答案了: Why isn't sizeof for a struct equal to the sum of sizeof of each member? (13 个回答) 已关闭 8
先生。 Stroustrup 在他的新书(TCPL 第 4 版)第 149 页写下了以下内容 1 N && sizeof(long)<=N任何 N 值的实现,更不用说任何人都会考虑使用 wchar_t
如 [5.3.3/3] 所述(expr.sizeof,工作草案): The sizeof operator can be applied to a pointer to a function, but
从C标准来看,int至少有16bit,long至少有32bit,long long如果有的话至少有64bit(有些平台可能不支持)。只是想知道标题中的句子是否总是正确的。 最佳答案 没有。该标准仅定义
我运行的是 Windows 7(64 位)。 这个问题与此处找到的问题相同: long on a 64 bit machine 但更深入,因为它处理更多的数据类型并适用到 C 或 C++,而不是 C#
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 8年前关闭。 Improve this
这个问题在这里已经有了答案: Length of array in function argument (9 个回答) 关闭 9 年前。 #include void printS(char []);
我承认这三个都有不同的含义。但是,我不明白这些具体情况适用于哪些特定情况。任何人都可以分享每个例子吗?谢谢。 malloc(sizeof(int)) malloc(size
To avoid things quietly breaking if you change the array size, I suggest std::copy(a, a + sizeof(a)/
我在 python 中注意到以下事实: >>> (1, 2, 3).__sizeof__() 48 >>> [1, 2, 3].__sizeof__() 64 我理解列表和元组之间的区别,但我希望它们
是否存在与指针大小相同的整数类型?保证所有微架构? 最佳答案 根据 this Wikipedia page ,在 C99 中,您的 stdint.h header 可能声明了 intptr_t 和 u
我注意到 int 和 double 的大小与使用函数 MPI_Type_size(MPI_INT, &MPI_INT_SIZE); 计算的不同。这是否意味着 sizeof(MPI_INT) 返回了错误
这个问题已经有答案了: How to find the size of an array (from a pointer pointing to the first element array)? (
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
我是一名优秀的程序员,十分优秀!