In languages such as C++, where you have to manage your own resources, you would usually use it to call delete or free on other objects you have allocated on the heap, or closing file handles you've created with fopen or such.īoth of this keywords work inside or outside a functions and methods, it does not matter where you call them from, they will always work with the heap. The destructor gives you a chance of doing some clean up in your code. The destructor is nothing more than a function that the language calls on the object when you do a delete my_object_pointer in your code. You have to remember to call delete (or delete p_array_of_object_pointers) for every object created with new. There is nothing magical with using new, at it's core it simply calls malloc, or some flavor of it depending on the platform, and then calls the constructor for the object. That applies to both complex objects such as objects from classes you or someone else has created, and built in types, such as int, char, double etc. In C++, every time you call new the object is created on the Heap and a pointer to that object is returned to you, you can then use that pointer to access your object. The heap on the other hand is a free store, by that I mean you can get any values from the heap no matter the order it was put in there - this distinction is important. The stack is a LIFO kind of data structure, meaning that the last thing you put in is the first you get from it. It is basically utilized to store parameters and function local data (among other things involved in the process of calling a function). The stack is a an area that the is used in function and method calls. It's called when the object goes out of scope for stack memory and when delete is called for heap memory. For example, if your class has raw pointers in it, you need to explicitly release them with delete in the dtor. One is provided by default for you, but there are times when you need to write your own. Had I not called delete, I would have lost access to *bar and would have gotten a memory leak.įor the second question, the role of the dtor is to clean up the object you've constructed. When foo returns, bar will go out of scope. It gets you the data pointed to by the pointer - is on the heap. *bar - yes, the asterisk does make a difference here. The pointer itself, bar, is created on the stack. *bar = 3 //sets the int being pointed to to 3īar = 0 //removes the now invalid reference Int *bar = new int //creates a pointer to an int on the stack, but initializes an int on the heap Failing to do so will result in a memory leak unless smart pointers are used.Īnother thing to note, take the following sample code: void foo() You must explicitly release this memory yourself in C++ via delete. These will persist in memory even after all pointers pointing to the memory have gone out of scope. Heap memory in allocated via the new operator. These are cleaned up once they've gone out of scope. Note that depending on what scope you're talking about, this memory could be global or it could be local to a function or class. Stack memory is what you said, but to be more specific, it's memory local to the current scope.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |