Delete a structure with nested structure instances does not work ?

I have two structures:
1
2
3
4
5
struct W1 {int v1;...};
struct W2 {
   W1 my_w1;
   .....
  };

Later I create an instance of W2
I have : W2 * my_w2 as private , and I create it so : my_w2 = new W2;

I have some functions to do the work :;
1 do
2.- Create the instance my_w2 = new w2;
3,4, ... 6general code to do some things.
7 w2->my_w1.w1 = 99;
8. delete my_w2;
9 print my:w2->my_w1.w1
// incredible I'd have to take an error but ... not
// I have : 99 ????
9 loop

I have discover that when delete my_w2 keeps my_w1 information ????
How can I delete my_w2 effectively ?
Thanks
Last edited on
when you use delete, the compiler arranges for the appropriate destructors to be called, then informs the OS that we are finished with the block of memory.

The OS merely marks the memory block as free - it does not have to do anything else and certainly it does not have to do anything immediately.

So if you do what you did, and immediately try to use the object again, you may find valid values.

It is up to you (the programmer) NOT to try to use the pointer again - you called delete and so
you should consider the object as DEAD
Thanks g.
But, How can I delete it?
(I have written the print my_w2->my_w1.w1 to know what is happen, but I dont need this instruction)
I have the my_w2 = new w2 and there is no NEW execution, because it keeps data .... no delete was executed efectivelly.....
Last edited on
The OS merely marks the memory block as free - it does not have to do anything else and certainly it does not have to do anything immediately.


Thus, tonnot, you can't force delete it. Why do you want to? You're not leaking memory, you don't need to be worried right now.
I have the my_w2 = new w2 and there is no NEW execution, because it keeps data ....
You are using the default constructor provided by the compiler, that just happen to do nothing.

By the way w2 my_w2; will create the object. You should use dynamic allocation when you want persistence outside the declaration scope.
Topic archived. No new replies allowed.