Domanda

I am just eager to know how STL library in C++ memory allocation and clear happens. For example, consider a list container containing objects of a class. The class may contain any data type and storage class (ex. static,const,dynamic). Will the memory of the container be the summation of objects inside the container? If I use the clear function will it destroy all the objects (using default destructor?) inside the container or just remove their references from the container? Please explain.

È stato utile?

Soluzione

The internals of stl containers, except std::array, are stored in heap, separately from the container object itself.

clear calls the destructor of whatever is stored in the container. If you store plain pointers or references to objects then no, the objects won't be destroyed, because pointers and references themselves don't have destructors. If you store unboxed objects or smart pointers, they will be destroyed.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top