为什么删除指向堆栈的指针是错误的? [英] Why delete pointer to stack is error?

查看:120
本文介绍了为什么删除指向堆栈的指针是错误的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读主题在堆栈上分配的变量上调用delete
而且我知道何时在堆栈中使用运算符删除,请参见错误。但是我想知道更多和更深入的信息。为什么会出错?

I have read topic Calling delete on variable allocated on the stack And i know when use operator delete in stack, see error. But i want to know more and deep information . Why error ?

推荐答案

在C ++运行时库中埋藏(不是很深)是一种称为堆的数据结构。堆的工作(从概念上来说,无论如何)是从一个巨大的字节数组开始的,并在程序要求时将数组的大部分分配给程序。这样一来,您不必自己实现任何块管理代码(这很好,因为在所有情况下正确有效地处理它都是不平凡的编码任务)。

Buried (not very deep) in the C++ runtime libraries is a data structure known as the heap. The heap's job (conceptually, anyway) is to start with a giant array of bytes, and dole out chunks of that array to your program when your program asks for them. That way you don't have to implement any chunk-management code yourself (which is good, because handling it both correctly and efficiently in all cases is a non-trivial coding task).

每当您的程序调用 new 运算符时,对堆代码的调用都会切分该数组的一部分(从概念上讲也是这样)并将其交给您的程序使用。相反,当您稍后调用 delete 运算符时,该字节子数组将返回给堆,在此堆的代码可以将其与合并的相邻部分合并在一起。大型数组(如果相邻部分中的一个或两个也都可用),或者至少在手边保留其可用性记录,以便稍后可以通过随后调用 new

Whenever your program calls the new operator, a call into the heap's code slices off a portion of that array (again, conceptually speaking) and hands it to your program to use. Conversely, when you later call the delete operator, that sub-array of bytes is given back to the heap, where the heap's code can merge it back together with its neighboring sections of big array (if one or both of the adjacent sections are also available) or at least keep a record of its availability on-hand so that it can be re-used later, via a subsequent call to new.

但是,堆栈上的对象并不位于堆的巨型数组内;而是它们位于堆栈上。因此,当您在堆栈对象上调用 delete 时,您正在向堆传递一块内存,该内存从一开始就不是其资源池的一部分。 C ++语言设计人员可能要求每个堆实现都必须检查这种情况并做出可预测的响应(例如,仅忽略未知指针,打印错误消息或触发断言失败并终止程序),但是像这样对每个 delete 的检查可能效率很低,并且C ++语言优先考虑在运行时使事情尽可能高效。因此,将指向 delete 的指针传递给以前未由 new 返回的未定义行为 ,这意味着C ++运行时库的设计人员不必关心在这种情况下他的堆代码将要做什么,因为这种情况一开始就永远不会发生-仅当您犯了编程错误时才会发生。因此,请不要犯该错误,因为它会导致程序以您不希望的方式运行。

Objects on the stack, however, are not located inside the heap's giant array; rather they are located on the stack. So when you call delete on a stack object, your are handing the heap a piece of memory that was never part of its resources-pool to begin with. The C++ language designers could have required that every heap implementation must check for this condition and do something predictable in response (for example just ignoring the unknown pointer, or printing an error message, or triggering an assertion failure and terminating the program), but doing a check like that for every delete could be inefficient, and the C++ language prioritizes keeping things as efficient as possible at run-time. Therefore, passing a pointer to delete that was not previously returned by new invokes undefined behavior, which means that the designer of the C++ runtime library does not have to care what his heap code will do in that situation, because that situation should never happen in the first place -- it will only happen if you made a programming error. So, don't make that error, because it will cause your program to behave in ways you did not intend it to.

这篇关于为什么删除指向堆栈的指针是错误的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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