简体   繁体   中英

Why delete pointer to stack is error?

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 ?

Stack created objects are automatic whereas non-stack created objects (using the keyword new ) are dynamic and need to be reclaimed using the keyword delete .

Objects on the stack are reclaimed by the system automatically using a different mechanism to objects allocated dynamically using keyword new .

So calling keyword delete with an object address that was not allocated dynamically using the keyword new is bound to cause trouble.

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).

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 .

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.

To understand it correctly you should first understand what stack is and what heap mean. Go through this to understand how process memory is aligned.

When you call a function, it receives contiguous memory chunk from the virtual momory of the process. which it uses to receive arguments, define local variables and store return address etc. This memory is freed once the function completes its execution and returns. This is called stack.

Where as the heap allocation is done at runtime. and hence it is not guaranteed to be contiguous. new function call finds suitable memory chunk for you from the free pool and delete returns it back to the free memory pool.

As stack and heap are physically different entities, operations defined on heap are not supposed to be executed on stack as this will cause undefined behavior at runtime as explained by Jeremy Friesner . As a thumb rule, it is better to report error as early as possible, which is what compiler does when it finds delete called upon object define on stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM