是否可以通过“deconstify / const_cast”更改地址? [英] Is an address change possible by "deconstify/const_cast"?

查看:63
本文介绍了是否可以通过“deconstify / const_cast”更改地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣知道如果相应类型的常量是

抛弃,编译器是否可以更改内存地址的指针值




inline char * deconstify1(char const * X){return(char *)X; } $ / $
inline char * deconstify2(char const * X){return const_cast< char *>(X);

}


const char source [] =" Test";

char * alias1 = deconstify1(source);

char * alias2 = deconstify2(source);


if((source!= alias1)||(source!= alias2)||(alias1!= alias2))

{printf(" unequal:% p%p%p",source,alias1,alias2);这是否可以在当前平台上发生这种情况?

是否有任何工具尝试将数据复制到只读区域以实现任何目的?

保护?


问候,

Markus

I am interested to know if the pointer value for the memory address can
be changed by a compiler if the constness of the corresponding type is
cast away.

inline char* deconstify1(char const * X) { return (char*) X; }
inline char* deconstify2(char const * X) { return const_cast<char*>(X);
}

const char source [] = "Test";
char* alias1 = deconstify1(source);
char* alias2 = deconstify2(source);

if ((source != alias1) || (source != alias2) || (alias1 != alias2))
{ printf("unequal: %p %p %p", source, alias1, alias2); }

Can this happen on a current platform?
Does any tool try to copy the data to a read-only area to achieve any
protection?

Regards,
Markus

推荐答案

Ma ************ @ web.de 写道:
Ma************@web.de wrote:
我很想知道如果相应类型的常量是
,编译器是否可以更改内存地址的指针值。抛弃。

内联char * deconstify1(char const * X){return(char *)X;内联char * deconstify2(char const * X){return const_cast< char *>(X);
}
const char source [] =" Test" ;
char * alias1 = deconstify1(source);
char * alias2 = deconstify2(source);

if((source!= alias1)||(source!= alias2) )||(alias1!= alias2))
{printf(" unequal:%p%p%p",source,alias1,alias2); }

这可以在当前平台上发生吗?


否。指针上的const_cast结果必须引用与原始指针相同的对象



注意写入最初声明的对象的结果

const是未定义的。

是否有任何工具尝试将数据复制到只读区域以实现任何<保护?
I am interested to know if the pointer value for the memory address can
be changed by a compiler if the constness of the corresponding type is
cast away.

inline char* deconstify1(char const * X) { return (char*) X; }
inline char* deconstify2(char const * X) { return const_cast<char*>(X);
}

const char source [] = "Test";
char* alias1 = deconstify1(source);
char* alias2 = deconstify2(source);

if ((source != alias1) || (source != alias2) || (alias1 != alias2))
{ printf("unequal: %p %p %p", source, alias1, alias2); }

Can this happen on a current platform?
No. The result of a const_cast on a pointer must refer to the same object as
the original pointer.
Note that the result of writing to an object that was initially declared
const is undefined.
Does any tool try to copy the data to a read-only area to achieve any
protection?




是的。还有一些平台,这些数据最初只能以只读的形式存在。



Yes. There are also platforms where such data initially lives in read-only
memory.


Ma ************ @ web.de 写道:
I我很想知道如果相应类型的常量被抛弃,编译器是否可以更改内存地址的指针值。
I am interested to know if the pointer value for the memory address can
be changed by a compiler if the constness of the corresponding type is
cast away.




不在C中(我不知道C ++)。第6.3.2.1节

第2段:如果左值具有合格类型,则该值具有左值类型的非限定版本的

; [...]"因此,

指向某个对象的const限定指针的值等于

指向同一对象的非限定指针的值。这可能是代表可能不同(不要因为禁止这个而错误地阅读6.2.5 / 25),但是`==''必须持有。


-

Eric Sosman
es ***** @ acm-dot-org.inva lid



Not in C (I don''t know about C++). Section 6.3.2.1
paragraph 2: "If the lvalue has qualified type, the value has
the unqualified version of the type of the lvalue; [...]" Thus,
the value of a const-qualified pointer to some object is equal to
the value of an unqualified pointer to the same object. It''s
possible that the representation could be different (do not
mis-read 6.2.5/25 as prohibiting this), but `=='' must hold.

--
Eric Sosman
es*****@acm-dot-org.invalid


Ma ************ @ web.de 写道:
我有兴趣知道是否如果相应类型的常量被抛弃,编译器可以更改内存地址的指针值。


这些位可能会改变,但它的价值不可能。即你可以使用

相等/关系运算符来比较它,但是你不能使用memcpy。

然而你可以使用memmove / memcpy来复制比特但是。

inline char * deconstify1(char const * X){return(char *)X;内联char * deconstify2(char const * X){return const_cast< char *>(X);
}


这两个意思完全相同事情。

const char source [] =" Test";
char * alias1 = deconstify1(source);
char * alias2 = deconstify2(source);

if((source!= alias1)||(source!= alias2)||(alias1!= alias2))
{printf(" unequal:%p%p%p",source ,alias1,alias2); }


我认为你的意思是&&而不是||。即使使用&&,此示例也总是评估为真。你的例子甚至没有回答你真正的b $ b意思是什么。你真正的意思是:


const char source [] =" Test";

const char * p1 = source;

char * p2 = deconstify1(source);

const char * p3;


memcpy(& p3,& p2,sizeof(p2) );


p1 == p3


答案是:是的,这保证是真的,因为

限定符只能根据类型系统更改访问级别,

而不是指针中位的解释方式:


3.9.2 / 3指向符合cv资格和cv不合格版本(3.9.3)

布局兼容类型的指针应具有相同的值表示和

对齐要求(3.9) 。


^ - 这是一个更强大的保证,表示

相同


3.9。 3/1类型的cv合格或cv不合格版本是

不同类型;但是,它们应具有相同的表示形式和

对齐要求(3.9).50)

3.9.3 / 1#50)相同的表示和对齐要求是

意味着可互换性作为函数的参数,从函数和工会成员返回

值。


^ - 这是额外的说,即使是指针上的顶级const也有相同的表示,所以我们可以改变const char * p1。 toconst

char * const p1"它仍然可以工作。


这可以在当前平台上发生吗?
是否有任何工具尝试将数据复制到只读区域以实现任何保护?
I am interested to know if the pointer value for the memory address can
be changed by a compiler if the constness of the corresponding type is
cast away.
The bits may change, but it''s value can''t. i.e. you can use the
equality/relational operators to compare it, but you can''t use memcpy.
You can however use memmove/memcpy to copy the bits over though.
inline char* deconstify1(char const * X) { return (char*) X; }
inline char* deconstify2(char const * X) { return const_cast<char*>(X);
}
These two mean the exact same thing.
const char source [] = "Test";
char* alias1 = deconstify1(source);
char* alias2 = deconstify2(source);

if ((source != alias1) || (source != alias2) || (alias1 != alias2))
{ printf("unequal: %p %p %p", source, alias1, alias2); }
I think you meant && and not ||. Even with &&, this example will always
evaluate to true. Your example doesn''t even answer what you really
mean. What you really meant was:

const char source [] = "Test";
const char *p1 = source;
char *p2 = deconstify1(source);
const char *p3;

memcpy(&p3, &p2, sizeof(p2));

p1 == p3

And the answer is: yes, this is guaranteed to be true because
qualifiers only change the level of access in terms of the type system,
not the way the bits are interpreted in a pointer:

3.9.2/3 Pointers to cv-qualified and cv-unqualified versions (3.9.3) of
layout-compatible types shall have the same value representation and
alignment requirements (3.9).

^-- this is an even stronger guarantee about the representation being
the same

3.9.3/1 The cv-qualified or cv-unqualified versions of a type are
distinct types; however, they shall have the same representation and
alignment requirements (3.9).50)
3.9.3/1#50) The same representation and alignment requirements are
meant to imply interchangeability as arguments to functions, return
values from functions, and members of unions.

^-- this is extra to say that even the top level const on pointers have
the same representation, so we can change "const char *p1" to "const
char * const p1" and it will still work.

Can this happen on a current platform?
Does any tool try to copy the data to a read-only area to achieve any
protection?




我不明白你在问什么。允许任何const对象[*]

存储在只读存储器中。你可以抛弃const并从中读取
,但是对该存储位置的任何写入都会导致未定义的

行为。

[*] top级别consts,即int * const var允许存储在

只读存储器中,但是const int * var不是。



I don''t understand what you''re asking. Any const object[*] is allowed
to be stored in read only memory. You can cast away constness and read
from it, but any write to that storage location results in undefined
behavior.
[*] top level consts, i.e. "int * const var" is allowed to be stored in
read only memory but "const int *var" is not.


这篇关于是否可以通过“deconstify / const_cast”更改地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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