安全导出指针的整数表示 [英] Integer representation of safely derived pointer
问题描述
以下我在3.7.4.3/3节中遇到过:
整数值是安全的整数表示-derived
指针,如果它的类型至少与std :: intptr_tand一样大,它
是以下之一:
[.. 。]
- 添加或按位
操作的结果,其中一个操作数是
安全导出指针值的整数表示P
,如果由
reinterpret_cast
转换的结果将比较等于安全 - 派生
指针可从reinterpret_cast
计算。
$ b b
Ok,让 int * P = new int(1);
是一个指针, long p_int =< reinterpret_cast< long> (P);
他的整数表示。考虑下面的代码:
int * P = new int(1); // Safely-derived pointer
long p_int =< reinterpret_cast< long>(P); //安全派生指针的整数表示
long new_p_int = p_int + 10; //一个加法运算的结果
void * new_P = reinterpret_cast< void *>(new_p_int);
void * P_cpnverting_to_void = reinterpret_cast< void *>(P);
cout< P converted to void * =<< P_cpnverting_to_void< endl;
cout< P在加法运算的结果之后=< new_P<< endl; rel =nofollow> demo
规则不清楚。结果指针如何比较等于reinterpret_cast(P)?它们在应用附加操作之后不会相等。
解决方案关键点是 reinterpret_cast< ; void *>
必须比较等于安全派生的指针可从计算 reinterpret_cast< void *>(P)
。
它不必等于 reinterpret_cast< void *>(P)
。
$ b考虑下面的例子,其中我们表现出与从 reinterpret_cast (P)
计算的安全派生的指针的相等性。 / p>
#include< iostream>
#include< stdint.h>
using namespace std;
int main(){
int * P = new int(1); //安全派生的指针
uint64_t p_int = reinterpret_cast< uint64_t>(P);
uint64_t new_p_int = p_int + 10; //加和运算的结果
void * new_P = reinterpret_cast< void *>(new_p_int);
void * P_computed_from_void_star = reinterpret_cast< void *>(P)+ 10;
cout<< P converted to void * =<< P_computed_from_void_star<< endl;
cout<< P在加法运算的结果之后=< new_P<< endl;
}
The following I have come across in the section 3.7.4.3/3:
An integer value is an integer representation of a safely-derived
pointer only if its type is at least as large as std::intptr_tand it
is one of the following:
[...]
— the result of an additive or bitwise
operation, one of whose operands is an integer representation of a
safely-derived pointer value P
, if that result converted by
reinterpret_cast<void*>
would compare equal to a safely-derived
pointer computable from reinterpret_cast<void*>(P)
.
Ok, let int *P = new int(1);
be a some pointer and long p_int = <reinterpret_cast<long>(P);
his integer representation. Consider the following code:
int *P = new int(1); //Safely-derived pointer
long p_int = <reinterpret_cast<long>(P); //Integer representation of safely derived pointer
long new_p_int = p_int + 10; // Result of an additive operation
void *new_P = reinterpret_cast<void*>(new_p_int);
void *P_cpnverting_to_void = reinterpret_cast<void*>(P);
cout << "P converted to void* = " << P_cpnverting_to_void << endl;
cout << "P after result of an additive operation = " << new_P << endl;
The rule is not clear. How can the result pointer be compare equal reinterpret_cast(P)? They never be compare equal after applying additive operation. Could you possibly provide actual example reflecting the rule?
解决方案 The key point is the pointer resulting from reinterpret_cast<void*>
must compare equal to a safely-derived pointer computable from reinterpret_cast<void*>(P)
.
It does not have to equal reinterpret_cast<void*>(P)
.
Consider the following example, where we exhibit equality with a safely derived pointer which computed from reinterpret_cast<void*>(P)
.
#include <iostream>
#include <stdint.h>
using namespace std;
int main(){
int *P = new int(1); //Safely-derived pointer
uint64_t p_int = reinterpret_cast<uint64_t>(P);
uint64_t new_p_int = p_int + 10; // Result of an additive operation
void *new_P = reinterpret_cast<void*>(new_p_int);
void *P_computed_from_void_star = reinterpret_cast<void*>(P) + 10;
cout << "P converted to void* = " << P_computed_from_void_star << endl;
cout << "P after result of an additive operation = " << new_P << endl;
}
这篇关于安全导出指针的整数表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!