delete pointer inside ?

This is a discussion on delete pointer inside ? within the C++ Programming forums, part of the General Programming Boards category; howdy~
we can delete pointer like
Code:
delete pt;
and what about if a pointer has another pointer inside ? ...

t is just a pointer to a test object. Your code does not tell t to point to any object. If you wrote Test * t = Test_Object (Where Test_Object was already defined in your code) deleting it should not delete the object.

Deleting the pointer to the allocated object will call that objects destructor. If the object itself has allocated any memory, the destructor has to delete this memory, or it will float around forever.

All memory allocated inside the class should be deleted in the destructor.

Deleting the pointer to the allocated object will call that objects destructor. If the object itself has allocated any memory, the destructor has to delete this memory, or it will float around forever.

All memory allocated inside the class should be deleted in the destructor.

so if we forget to delete the pointer whin the body of Test's destructor will result in lack of memory and crash our machine, didnt i go wrongly here ?

>so if we forget to delete the pointer whin the body of Test's destructor will result in lack of memory and crash our machine
Not necessarily. If you fail to release the memory pointed to by the pointer before the object is destroyed then yes, you'll have a memory leak. But that alone won't crash your machine unless you create and destroy large numbers of objects of this class in a program that runs for long periods of time.

If you only create and destroy a few objects then the leak won't be bad and you'll have no ill effect to the rest of the program unless it uses a large amount of memory elsewhere and those few bytes are critical later in the execution. If the program doesn't run for a long time then when it terminates, the operating system (assuming a hosted implementation that actually does this) will reclaim the memory used by your process and all will be well.

Still, it's a good idea to clean up after yourself because useful programs that weren't meant to be run often or for long periods of time tend to be reused as libraries in applications that do.

"I want to delete a pointer" is a common way to say "I want to delete the memory a pointer points to". But, I think it's important to point out that we don't delete pointers, we delete memory that we declare. We keep track of memory that we declare by using pointers, but deleting the memory is not the same as deleting the pointer.