C ++ Destroy objects in a fix

8

Hi, I have a couple of questions

Let's say I have this code:

class alumno
{
    private:
        string nombres;
        char telefono[12];
        int nivel;
    public:
        alumno();
        ~alumno();
}

and then in the main declare

alumno estudiante[10];

My doubts are:

  • How do I call the destructor for a single object?
  • How do I destroy the whole arrangement?
  • If I delete an object, the data remains blank? or disappears object box? (So if the size of the array is 10, it will go down to 9?)
asked by Malthael 13.02.2017 в 18:43
source

2 answers

8

1. How do I call the destructor for a single object?

Within an arrangement, you can do estudiante[3].~alumno( );

2. How do I destroy the whole arrangement?

As you said, the fix is destroyed when you exit the function, automatically. Before said destruction , they are invoked, one by one, the destructors of the objects contained in the array.

If you want to be able to destroy it when it pleases you, you have to use dynamic memory:

alumno *estudiante = new alumno[10];
...
delete[] alumno;

If you use this last formula, dynamic fixes , remember that it is your responsibility to destroy them by doing delete[] . The compiler totally disregards them. That is, they are not destroyed or released when leaving the function.

3. If I delete an object, the data remains blank? or the object box disappears? (So if the size of the array is 10, it will go down to 9?)

If the instances are within an array , neither the array nor the data changes. Everything remains as it is when the destructor of the class involved ends.

If the destroyer does nothing, the memory remains as it was. Of course, the number of elements in the array does not change .

Think that, from the compiler's point of view, a array is nothing but a block of memory . Once assigned , it does not change in size (unless we change it explicitly).

Care with what you do. By logic, if you first call the destructor of an object, and then do the delete of the complete array, the destructor will be called again . It is up to you to check the logic of your program.

    
answered by 13.02.2017 / 19:32
source
4
  

What do I call the destructor for a single object?

Inside the vector the objects are stored by value, then it is not necessary to call the destructor of an object. Among other things, the memory that this element occupies within the vector is not going to be released.

Calling destroyers directly is something to avoid except for very justified reasons. If you program with certain criteria you will realize that it is not necessary to call them explicitly.

  

How do I destroy the whole arrangement?

alumno estudiante[10];

The fix is created without using dynamic memory, then you can not destroy it manually. When the execution leaves the scope of the arrangement, the program will automatically call the destructor of each object in the array.

A different case would be if a dynamic memory reservation is made. In which case it becomes necessary to release the memory by hand:

alumno* estudiantes = new alumno[10];

delete[] estudiantes;

Why is this behavior so disparate?

The fix that you have created is stored in the stack of the program or stack . The stack is managed by the program itself since it uses it to store the function jumps, then the program knows that it must eliminate the variables.

When you use dynamic memory you become responsible for the management of that memory.

  

If I delete an object, the data remains blank? or the object box disappears? (So if the size of the array is 10, it will go down to 9?)

No. Since the object does not use dynamic memory, it does not manage unique resources such as files, sockets, etc. You will not notice any difference . The destructor is just one more function that the compiler knows to call in certain circumstances, such as the variable goes out of scope or a call is made to delete .

    
answered by 13.02.2017 в 20:49