C ++: delete structure?

I have a structure containing pointers:

struct foo { char* f; int* d; wchar* m; } 

I have a vector of generic pointers to these structures:

 vector<shared_ptr<foo>> vec; 

vec stands out on the stack. When it ends at the end of the method, its destructor will be called. (Correct?) This, in turn, will call the destructor of each element in the vector. (Right?) Does the delete foo call only call pointers like foo.f , or does it really free memory from the heap?

+6
c ++ memory-management heap
source share
6 answers
 delete foo; 

will delete the memory allocated for the foo structure, which includes three pointers. But the memory that the pointers themselves point to will only be deleted if you implement a destructor that explicitly deletes them.

+15
source share

If you dynamically allocated foo , for example:

 foo* f = new foo; 

then delete f destroy the dynamically allocated object foo , including the pointers that it contains, but does not point to pointers if they really point to dynamically allocated objects or arrays of objects.

If you assigned a dynamically allocated foo object (i.e. the result of new foo ) to shared_ptr (assuming tr1 or boost), then when the last shared_ptr referencing this object goes beyond delete will be called by the pointer originally returned by new foo automatically . You do not need to do this manually.

If your object ( foo ) contains pointers to dynamically allocated objects that it owns (so you need to free it at the end of the foo life cycle), then it is highly recommended to write a destructor to free these objects (which will depend on how they are distributed).

After you write the destructor, you need to think about whether you need to write a copy constructor and a copy operator. When you use a vector of generic pointers, you can decide that your objects should not be copied. If so, you can declare them closed and not need to be implemented.

You should also consider one or more constructors to make sure your pointer elements are initialized. If the pointer elements are never initialized, then if they are not assigned during the life of foo , then they will be neither null nor exact for the actual object, and this may cause an error in your destructor.

+10
source share

It removes only pointers.

+1
source share

You do not call delete f , where f is an object of type foo if f is the stack allocated. You also do not call delete f on a heap of an object of type foo if this address is stored in a shared pointer. shared_ptr objets will call delete for you when the last link is freed.

Since your vector stores smart pointers, they will also go out of scope when your vector falls out of scope and the foo destructor will be called and the associated memory will be freed. However, foo memory has a size of 3 pointers. Not what these pointers contain.

If the foo members are dedicated heaps, you will need to delete them separately. For example, it is possible in the destructor foo if the memory they are pointing to is not shared between objects.


When it goes out of scope at the end of the method, its destructor will be called. (Right?)

Right

This, in turn, will call the destructor of each element in the vector. (Right?)

Right Smart pointer destructor.

or does it really free memory from the heap?

It calls the smart pointer destructor, if there are no more references to this pointer, then the object contained in it will be deleted and its memory will be freed.

+1
source share

There seems to be a terminological confusion in your question. The vector destructor will call the destructors for shared_ptr elements, which in turn will delete internally on their stored pointers (if necessary).

A call to delete foo , where foo is a pointer to struct foo , will call the struct foo destructor and then free the memory occupied by *foo .

The destructor above struct foo does nothing. This is trivial. If no attempt is made to free the memory indicated by struct foo::f or any other members. Why? He does not know and cannot know whether this memory should be freed.

In fact, since struct foo::~foo trivial, compilers usually don't even try to call it.

+1
source share

When it [vec] goes out of scope at the end of the method, its destructor will be called. (Correctly?)
Correctly

This, in turn, will call the destructor of each element in the vector. (Correctly?)
Correctly, this will remove the shared_ptr elements in the container, and if they are the last, also the elements they share.

Make a call to delete foo ...?
You cannot delete foo, it is a structure. You can remove the foo instance.
The call removes the calls to the destructor and frees memory for the foo structure.

Does foo destructor only remove pointers such as foo :: f, or does it really free memory from the heap?
It depends on the destruction, in this case you have a default destructor and so ... No. In the following code example, you can see some of the reasons why the default destructor associated with foo cannot automatically clear any pointer elements.

 { char ca='a', *cb=new char; int *i = (int*)malloc(sizeof(int)); foo a; shared_ptr<foo> b = new foo(); af = &ca; ad = i; bf = cb; } 
0
source share

All Articles