destructors

closed account (SECMoG1T)
Hey there, i need help to understand a statement i came across about class & structs involving dynamic memory.

The statement read "Destructors are automatically called when an object of such a class goes out of scope ", mmhh?

Is this supposed to mean that when an object of a class are used the programmer should not bother calling DELETE;

OR does it mean that immediately DELETE {//might be on the object member pointer } is called for an object of such a class , The destructor is automatically called???

Thenks in advance.
Last edited on
You are talking about a pointer. The pointer doesn't have a destructor so nothing will happen when it goes out of scope. If you want to destroy the object that you created with new you should use delete, and that will in turn call the destructor of the object.
OR, be modern and use smart pointers. Then yes, once the pointer goes out of scope the memory is automatically freed, so you don't worry about calling delete.
closed account (SECMoG1T)
#@peter , Thanks for corrections . What i tried to mean is, if my my class uses new to allocate memory and i av already made a ~destructor for the class .

How will my destructor work "automatically"??? . calling delete ,then destructor ; or once my program notices that an object is no longer needed it will automatically call the destructor to free the memo allocated with new.

But thanks you'av already mentioned the whole concept ;

@residentBiscuit
Am not aware of these smart pointers .

What library do i need to have them? How do i use 'em? Are they template pointers ,i mean can i use them with any type of data e.g int, doubles,strings etc.?

Well i am more than willing to get more about them coz they seem to simplify my problem
Smart pointers can be found in the STL under #include <memory> though they will require a C++11 compiler which you most likely have. Yes they are templated and allow you to use any type you wish to.

The main two are

std::shared_ptr - http://www.cplusplus.com/reference/memory/shared_ptr/

std::unique_ptr - http://www.cplusplus.com/reference/memory/unique_ptr/

Here is a article on their use - http://www.codeproject.com/Articles/541067/Cplusplus-Smart-Pointers
Last edited on
closed account (SECMoG1T)
Thanks zereo for the help i am glad , lemmi check them out;
1
2
3
4
{
   Foo bar;
   //...
} //bar goes out of scope 
when an object goes out of scope, its destructor is invoked.

Now, suppose
1
2
3
4
5
6
{
   Foo *bar;
   //...
   bar = new Asdf();
   //...
}//bar goes out of scope 
there `bar' is a pointer, and the destructor of a pointer does basically nothing (so you've got a memory leak)

smart_pointer classes wrap around a pointer and manage the memory that it points to. Their destructor does have code to ensure proper resource release.
If the resources is managed in your class and you instantiate the class in static memory. Then you don't need to call delete.
For example, if you have the following class:
1
2
3
4
5
6
7
8
9
10
class IntPtr {
public:
    IntPtr() :
        ptr(new int()) {}
        
    ~IntPtr() { delete ptr; }
    
private:
int* ptr;
};

and you have an object of IntPtr:
IntPtr obj;
When obj goes out of scope, its destructor is called and the memory is freed. You don't need to call delete yourself.
closed account (SECMoG1T)
I think am now getting my solution right.

@ne555 you said when i use this

1
2
3
4
5
{Foo *bar;
   //...
   bar = new Asdf();
   //...
}//bar goes out of scope 

i will have to call delete on bar right ? to manange my memory?


1
2
3
4
5
6
7
8
9
10
class IntPtr {
public:
    IntPtr() :
        ptr(new int()) {}
        
    ~IntPtr() { delete ptr; }
    
private:
int* ptr;
};



@Nikko gave me this example n i got that when an an obj of such a class is out of scope
i will not need to call delete as that is already taken care of by the destructor . is that true?

Thanks for the help . But still one more clarification ?

(1) What if i have a member function for the class nikko gave thet returns a pointer to a new object e.g

1
2
3
4
5
6
7
8
intPtr* newObject()
           {
              intPtr* Temp_ptr;
              Temp_ptr= new intPtr;
              /// here might be i change the objects properties
              /// e.g Temp->size=50; /// some more code;
               return Temp_ptr;
           }

How do i free such memory if object is out of scope??? will i have to call delete for each object allocated this way if it is out of scope , or the destructor takes care of this needless of my effort???
Last edited on
when an an obj of such a class is out of scope
i will not need to call delete as that is already taken care of by the destructor . is that true?

Yes, if it is a plain object.

For the function, you should free the memory manually after using it.
1
2
3
4
IntPtr* dynAlloc = newObject();
// Do something with dynAlloc
delete dynAlloc; // call the destructor of the underlying object
dynAlloc = nullptr; // for safe, otherwise dynAlloc would be a dangling pointer 


You may want to learn more by reading http://www.learncpp.com/cpp-tutorial/69-dynamic-memory-allocation-with-new-and-delete/

As stated above, smart pointers are safer to use.
1
2
3
4
5
6
std::unique_ptr<IntPtr> newObj()
{
	std::unique_ptr<IntPtr> temp(new IntPtr());
	// do something with temp...
	return temp;
}

1
2
std::unique_ptr<IntPtr> obj = newObj();
// no need to delete obj 

Here, you can use obj almost as the same way you use dynAlloc ("almost" because you cannot assign one unique_ptr to another).
When unique_ptr goes out of scope, delete is call by the default "deleter" (not by you).
Last edited on
closed account (SECMoG1T)
Thank you for settling my doubts , I think now am in tha right track .
smart pointers are now my choice , guss they are more friendly haha.
Topic archived. No new replies allowed.