How does =delete on destructor prevents allocation?

南楼画角 提交于 2020-01-19 05:09:16

问题


In this SO question is stated that this construct prevents stack allocation of instance.

class FS_Only {
    ~FS_Only() = delete;  // disallow stack allocation
};

My question is, how does it prevents allocation? I understand, that is not possible to delete this instance, either explicitly or implicitly. But I think, that would lead to memory leak or run time error, respectively.

Are compilers smart enough to sort this out and raise compiler error? Also why would one need this?


回答1:


The destructor of a variable with automatic storage duration (i.e. a local variable) would need to run when the variable's lifetime ends. If there is no accessible destructor the compiler refuses to compile the code that allocates such a variable.

Basically the distinction between "stack allocation" (an inaccurate choice of term by the way) and free store allocation is that with local variables constructor/destructor calls always come in pairs, whereas with free store allocation you can construct an object without ever destructing it. Therefore by preventing access to the destructor your code makes it impossible to allocate a local variable of the type (if the constructor runs the destructor must also run, but there is no destructor so the program is rejected).




回答2:


I understand, that is not possible to delete this instance, either explicitly or implicitly.

More than that, it's not possible to destroy any instance; whether by deleting it or otherwise.

Declaring an automatic variable (or "stack allocation", if you like) doesn't just cause the instance to be created when the program reaches the point of declaration; it also causes it to be destroyed when the program leaves that block. With a deleted destructor, that can't be done, so the declaration is not allowed.

This also prevents you from declaring a static or thread-local variable, since that also generates code to destroy the variable when the program or thread ends.

So the only way to create one of these is with new, and once you've done that you can never destroy it. However, this doesn't altogether prevent stack allocation:

char memory[sizeof(FS_Only)] alignas(FS_Only);
FS_Only * not_fs = new (memory) FS_Only;

Also why would one need this?

In my view, you wouldn't. A mandatory memory leak is a horrible way to ensure that an object is never destroyed at the wrong time. Instead, use techniques such as RAII to manage any objects that need a dynamic lifetime, ensuring that they always have a well defined owner (or shared owners) responsible for deleting them after use. The smart pointers in the C++11 standard library are a good starting point.




回答3:


Marking a destructor as deleted will make it impossible to destroy the object. It doesn't matter if it's on the stack or on the heap. All destruction of an object (whether automatic by it going out of scope, or by doing delete on it) calls the destructor. And as it is the compiler that handles the calling of the destructor, it will notice if it's been marked as deleted and issue an error.

It doesn't however disallow the creation of objects, but as non-pointer instances are destructed on the end of the scope where they were declared, the compiler will issue an error, in effect disallowing creation of the instance. It's still possible to dynamically allocate instances using new, with the caveat that they can't be deleted, possibly leading to a memory leak.




回答4:


I have two answers:


From "The C++ Programming Language":

You can’t have a local variable that can’t be destroyed (§17.2.2) ...


In my own words:

Think about it for a minute - a variable created on the stack is meant to be deleted at the end of scope in which it is declared.

By disallowing the object's implicit destructor, you are basically telling the compiler "You are not permitted to delete this object at the end of scope." The compiler doesn't want to take a risk of creating something that can't be destroyed (and it's right - what if it's large and will leak megabytes of memory?), so it refuses to create it. That leaves you only one option - to create it on the free store and manually manage it.

Now the responsibility is yours. Note, however, that this is bad code - the objects will never be able to be deleted, even manually. Remember - you deleted the destructor! As it was correctly pointed out in other answers, this is a forced memory leak and should be avoided.




回答5:


When you try to declare FS_Only on the stack

{ 
    FS_Only fs;
    //...
}

the destructor will automatically be called on the closing brace so you will get a compile error.
Of course, you won't be able to delete a new'ed one either:

{ 
    FS_Only * fs = new FS_Only();
    //...
    delete fs; //also won't compile
}



回答6:


The only way to allocate such an object would be through dynamic allocation, without any de-alocation (the object will either be leaked, or accessed by pointer for the duration of the program.

Any attempt to allocate a static instance will cause a compilation error when the allocated object would go out of scope (as the destructor code cannot be called).



来源:https://stackoverflow.com/questions/18847739/how-does-delete-on-destructor-prevents-allocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!