一个堆分配的const对象与非const对象有什么不同? [英] How would a heap-allocated const object differ from non-const one?

查看:213
本文介绍了一个堆分配的const对象与非const对象有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,可以在堆上分配const对象:

  const Class * object = new const Class(); 
const_cast< Class *>(object) - > NonConstMethod(); // UB

,以便尝试写入对象将是UB。



我没有得到这样的对象将如何与一个未声明 const 的堆分配对象不同:

  const Class * object = new Class(); 

我的意思是当我在堆栈上分配一个对象到自动存储这是实现特定的,因此可能有一些实现特定的方法,将允许分配 const 对象以一些特殊的方式,将产生UB当我写一个对象。 / p>

然而每当我使用 new ,编译器需要发出 operator new code>函数调用,该函数不能做任何不同的事情 - 它只是以统一的方式分配内存,而不管我的代码中是否有 const 。 / p>

const 堆分配对象与非 - const

解决方案

没有区别 >对象

指向内存区域的变量的(编译时)类型有所不同。



这是语义摩擦:变量不同,数据位使用的实际内存是const / volatile不可知的。



对于一个非常有趣和启发





未定义行为



以非const方式处理const数据可能导致未定义行为,因为编译器允许根据知识做某些优化,const变量不会改变 1 。由于编译器的假设被积极地否定,所以改变它无关(例如通过 const_cast<> )可能导致错误的结果。



1 请注意,在可以同时修改const变量的情况下, volatile const 是否为本地不会/不能触及promis,而 volatile 说:'不要以为这不会改变,即使它没有被写入这个代码段'
`


In C++ it is possible to allocate a const object on heap:

const Class* object = new const Class();
const_cast<Class*>( object )->NonConstMethod(); // UB

so that attempt to write into an object will be UB.

I don't get how such an object will be different from a heap-allocated object that is not declared const:

const Class* object = new Class();

I mean when I allocate an object on stack it goes to automatic storage which is implementation-specific and so there might be some implementation-specific means that would allow allocating const objects in some special way that would yield UB when I write to an object.

Yet whenever I use new the compiler is required to emit operator new() function invokation and that function can't possibly do anything different - it just allocates memory in a uniform manner regardless of whether there was const in my code.

How is a const heap-allocated object different from a non-const one and how is undefined behavior possible if I try to modify it?

解决方案

There is no difference in the object. There is a difference in the (compile-time) type of the variable(s) used to refer to the memory area.

This is semantic friction only: the variable is different, the actual memory used by the data bits is const/volatile agnostic.

For a very amusing and enlightening story describing similar semantic friction see this all-time-favourite answer by Eric Lippert:

On the Undefined Behaviour

Treating const data in a non-const way can lead to Undefined Behaviour, because the compiler is allowed to do certain optimizations based on the knowledge that a const variable won't change1. Changing it none-the-less (e.g. by const_cast<>) can lead to erronous results because the compiler's assumptions are actively negated.

1 Note that volatile is there to help in cases where const variables can get modified concurrently. You can see how const is a 'local' won't/can't touch promis, whereas volatile says: 'don't assume this won't change, even though it is not being written to in this code segment'. `

这篇关于一个堆分配的const对象与非const对象有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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