'i' remains as 33 after executing the destructor

Hi The following programme is from a video tutorial.

I think/learnt the aim of the destructor is to release the memory of the constructor. Since that, I don't understand why 'i' remains as 33 after executing the destructor.

Many thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
class A
{
public: 
	A(int x){i=x;cout<<"constructor is executing!"<<i<<endl;}
        ~A(){cout<<"destructor is executing!"<<i<<endl;}
	void get(){cout<<i<<endl;}
private:
	int i;
};

int main()
{
A a(33);
a.get();
return 0;

}
Last edited on
Since that, I don't understand why 'i' remains as 33 after executing the destructor.
As your output statement clearly says, "destructor is executing!" If it is still executing, then it's not yet after it has executed.

Don't learn programming from videos. They're a lousy format for it.
As your output statement clearly says, "destructor is executing!" If it is still executing, then it's not yet after it has executed.


Thank you for your response.

The statement of the output was typed by the teacher. He could type any content for the output.

With the constructor, 'i' is saved at the memory.

After executing the constructor, the programme will execute the destructor to release the memory.

But why does 'i' remain as 33?
Last edited on
> the aim of the destructor is to release the memory
> he programme will execute the destructor to release the memory.

No. The destructor does not release the memory that was occupied by the destroyed object.

Accessing an object after it has been destroyed is undefined behaviour; though accessing (re-using) the memory that an object was placed in after the object is destroyed may be valid.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

struct A { int value ; ~A() { std::cout << "destroy object at address " << this << '\n' ; } };

int main()
{
    char buffer[ sizeof(A) ] ;

    A* pa = new(buffer) A{123456} ; // construct an object of type A
    std::cout << "object of type A at address " << pa << " has the value {" << pa->value << "}\n" ;

    pa->~A() ; // destroy the object

    buffer[0] = 'X' ; // reuse memory
    std::cout << "object of type char at address " << (const void*)buffer << " has the value '" << *buffer << "'\n" ;
}

http://coliru.stacked-crooked.com/a/33b2ba5c1e913fc9
http://rextester.com/GFXZK89681
Thank you very much for your help, JLBorges.

Are the following correct?

-destroy an object= destroy an object at address?

-destroy an object at address= the memory which was occupied by the destroyed object is now free, and but the data still remains?

-Is it like a tentant (the destroyed object) moved away from the house (memory/address), but the furniture(data, 33) remains?

PS. I don't fully understand the programme you cited because I am a very beginner. For example, I haven't learnt array, and pointer is still difficult for me.
Last edited on
> -destroy an object= destroy an object at address?

Yes. In somewhat simplified terms, an object has a type (for example int) and is associated with a region of storage (it is placed in some area of memory).

Constructor: initialises an object; converts uninitialised storage (memory) into an object.
Destructor: destroys an object; after the destructor executes, what is left is uninitialised storage.

The storage (memory) used by an object must be made available (allocated) as uninitialised storage before the constructor begins execution. (Typically this is automatically done by the implementation.)

Immediately after the destructor finishes executing, the storage (memory) which was used by an object would be left behind; it is now uninitialised storage. (The implementation may deallocate this memory immediately after the destructor returns.)


> destroy an object at address= the memory which was occupied by the destroyed object is now free,

Yes; the memory which was occupied by the destroyed object may be reclaimed by the implementation or reused for other purposes either immediately or at a later stage.


> but the data still remains?

The 'data' has a type and therefore is always associated with an object; technically it does not exist after the object has been destroyed. However, the memory that is left behind may (or may not) contain the 'foot-print' of the object.


> Is it like a tentant (the destroyed object) moved away from the house (memory/address),
> but the furniture(data, 33) remains?

Yes, it is somewhat like that. However, if the tenant goes back to the house some time later, there is no guarantee that the furniture would still be in the same place where it was left when the tenant moved out.


> I am a very beginner.

Do you know something about functions? Have you encountered the keyword static?
If you have, an example that you can understand, which illustrates the above concept would be possible.
Hi JLBorges,

It's very kind of you to keep helping me.

I think I (at least basically) understood what you answered, that have solved my questions.

I've learnt some functions from the tutorial, but I haven't learnt "static" so far.

Much obliged! Have a good one! :)

PS. Though my questions have been solved, but I'd like to leave the thread open for a few days in case I might have further relevant questions, before marking "solved".


Last edited on
Topic archived. No new replies allowed.