gpt4 book ai didi

C - charArray 总是与 &charArray 相同吗?

转载 作者:太空狗 更新时间:2023-10-29 15:28:37 25 4
gpt4 key购买 nike

我正在阅读 an article about whole program optimization .链接时代码生成部分的最后一段说,将分配在堆栈上的数组归零可能不会产生相同的效果,具体取决于它的归零方式:

Turning on whole program optimization did uncover several bugs that had undefined behavior. Without WPO, these had somehow not crashed. With WPO, they did. In one case, a member function call was being made through a pointer to uninitialized memory. In several other cases, it was assumed that arrays on the stack were identical to their own addresses. That is, it was assumed that memset(&charArray, 0, sizeof(charArray)) would have the same effect as memset(charArray, 0, sizeof(charArray)). This is not guaranteed by the standard, and appears to change under WPO.

我认为如果我执行 char foo[1] foo 将始终是 ==&foo .有人可以解释这里发生了什么吗?谢谢

最佳答案

foo 是一个数组,在表达式中 foo 将转换为指向其第一个元素的指针,除非一元操作数 &sizeof 运算符。因此,在这种情况下 foo == &foo[0]&foo是数组foo的地址,不是foo的第一个元素的地址。
foo&foo 的值虽然等价,但类型不同。 foo 衰减后是char * 类型,而&foochar (*)[1] 类型。

关于C - charArray 总是与 &charArray 相同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37646367/

25 4 0
文章推荐: c - 这是将 va_arg 与函数指针一起使用的正确方法吗?
文章推荐: android - 如何在canvas中记录用户的操作并在android中保存为视频文件?
文章推荐: javascript - 将
文章推荐: C可变包装器
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com