为什么删除可以执行指针到const而free不能? [英] Why delete can perform on pointers to const while free cannot?

查看:181
本文介绍了为什么删除可以执行指针到const而free不能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到传递给 delete 的指针可以是 const 合格,而传递给 free 不能。这对我来说真是一个惊喜。

I've just noticed that the pointers passed to delete can be const qualified while those passed to free cannot. That is really a surprise to me.

在C ++中,操作符删除的重载应该有一个签名:

And in C++ an overload to operator delete should have a signature like:

void operator delete(void* p);

但是添加一个 const 无效:

void operator delete(void const* p);

任何人都可以告诉我为什么 delete 这种方式?

May anyone tell me why delete is designed this way?

推荐答案

free 不应该与实际的C ++对象。 free 应与 malloc 一起使用,因此您不应使用免费

free should not be used with actual C++ objects. free should be used with malloc, so you shouldn't be using free on something you allocated with new.

至于为什么你可以删除 const对象,很简单:

As to why you can delete const objects, that's simple:

const Type *ptr = new Type(...);

现在什么?如果您无法删除它,那么您必须这样做:

Now what? If you couldn't delete that, then you'd have to do this:

delete const_cast<Type*>(ptr);

有一个对象 const 不能修改。您不能导致对象从一个状态转到另一个状态。删除被丢弃。意味着它不再存在于任何状态,无论是原始的还是某些其他修改的形式。删除是存在于对象的可变性状态之外的操作,很像构造。

Having an object be const means that it cannot be modified. You cannot cause the object to go from one state to another state. Deletion is throwing it away. Meaning that it no longer exists in any state, whether the original one or some other modified form. Deletion is an operation that exists outside of the object's mutability state, much like construction.

概念上,删除既不是const也不是非const。虽然析构函数是一个非const函数,但是破坏一个对象的想法就在const或者非const的范围之外。

Conceptually, deletion is neither const nor non-const. While a destructor is a non-const function, the idea of destructing an object is simply outside the domain of const or non-const.

好吧,假设你定义 operator delete 来取一个指向const(与const指针不同)的指针

OK, let's say you define operator delete to take a pointer to const (which is different from a const pointer):

void operator delete(void const* p);

这个函数的第一行是什么? operator delete 的目的是释放由 operator new 分配的内存。这将需要戳在内存分配堆上的位。要做到这一点,你需要一个不指向const数据的指针:

What is the first line of this function going to be? The purpose of operator delete is to free memory that was allocated by operator new. That will require poking at bits on the memory allocation heap. And to do that, you need a pointer that does not point to const data:

void *ptr = const_cast<void*>(p);

欢迎使用未定义的行为 。虽然C ++允许你这样做,规范很清楚,尝试写入 ptr (或任何基于它的地址)的结果是 undefined 。你给了一个const指针;你被外界告诉不要修改它指向的东西。

Welcome to undefined behavior. While C++ allows you to do this, the specification is very clear that the results of attempting to write to ptr (or any address based on it) are undefined. You were given a const pointer; you were told by the outside world not to modify the stuff it pointed to. C++ provides no guarantees about what happens when you break that contract.

由于规范声明这是未定义的行为,因为 operator delete

Since the specification states that this is undefined behavior, and since operator delete (in most cases) cannot do its job without modifying the memory pointed to by p (or modifying memory based on that address), it would be silly of the specification to then allow you to define operator delete this way. It would basically be canonizing the idea of shooting yourself in the foot.

是的,在几乎所有的情况下,这将是完全安全的。但是,既然你要抛弃常量,为什么还要麻烦允许一个相当可疑的想法在第一位?

Yes, in virtually every case, this would be completely safe. But since you're going to cast away the const anyway, why even bother to allow the rather dubious idea in the first place?

这篇关于为什么删除可以执行指针到const而free不能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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