Dereferencing an invalid pointer, then taking the address of the result(取消引用无效指针,然后获取结果的地址)
问题描述
考虑:
int* ptr = (int*)0xDEADBEEF;
cout << (void*)&*ptr;
* 有多非法,因为它与直接的 & 结合使用,并且没有重载的 op&/op* 在玩?
How illegal is the *, given that it's used in conjunction with an immediate & and given that there are no overloaded op&/op* in play?
(这对于寻址过去结束的数组元素 &myArray[n] 具有特殊的影响,该表达式明确等效于 &*(myArray+n). 这个问答 解决了更广泛的问题,但我觉得它并没有真正满足上述问题.)
(This has particular ramifications for addressing a past-the-end array element &myArray[n], an expression which is explicitly equivalent to &*(myArray+n). This Q&A addresses the wider case but I don't feel that it ever really satisfied the above question.)
推荐答案
假设变量 `ptr' 不包含指向有效对象的指针,如果程序需要将表达式 `*ptr',在 [conv.lval](ISO/IEC 14882:2011,第 82 页,4.1 [#1])中指定.
Assuming the variable `ptr' does not contain a pointer to a valid object, the undefined behavior occurs if the program necessitates the lvalue-to-rvalue conversion of the expression `*ptr', as specified in [conv.lval] (ISO/IEC 14882:2011, page 82, 4.1 [#1]).
根据 [expr.unary.op] (ISO/IEC 14882:2011,第 109 页,5.3.1 [#3])
During the evaluation of `&*ptr' the program does not necessitate the lvalue-to-rvalue conversion of the subexpression `*ptr', according to [expr.unary.op] (ISO/IEC 14882:2011, page 109, 5.3.1 [#3])
因此,这是合法的.
这篇关于取消引用无效指针,然后获取结果的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:取消引用无效指针,然后获取结果的地址
基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
