引用无效的指针,然后获取结果的地址 [英] Dereferencing an invalid pointer, then taking the address of the result

查看:105
本文介绍了引用无效的指针,然后获取结果的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑:

  int * ptr =(int *)0xDEADBEEF; 
cout<< (void *)& * ptr;

* 它与立即& 结合使用,并且没有重载 op& / op * 在玩?






-end数组元素& myArray [n] ,一个明确等效于& *(myArray + n) c>。>

解决方案<>

/ div>

假设变量`ptr'不包含指向有效对象的指针,如果程序需要对表达式* ptr进行左值到右值转换,则会出现未定义的行为,如[conv .lval](ISO / IEC 14882:2011,第82页,4.1 [#1])。

在& * ptr的评估期间,根据[expr.unary.op](ISO / IEC 14882:2011,第109页,5.3.1 [#3])

$ b,需要对子表达式* ptr进行左值到右值转换
$ b

因此,这是合法的。


Consider:

int* ptr = (int*)0xDEADBEEF;
cout << (void*)&*ptr;

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?


(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.)

解决方案

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]).

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])

Hence, it is legal.

这篇关于引用无效的指针,然后获取结果的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆