Dynamic Memory Allocation Question

Started by
5 comments, last by Grudgian 22 years, 9 months ago
Hello, I have a question that has been bugging me. Why must we use the delete operator when dynamically allocating memory? Wouldn''t the memory be freed up when the object is destroyed? I know it has something to do with passing a pointer and it staying in memory, but why and how does this happening? Thanks, Chris
Advertisement
The memory is freed up when the object is destroyed - the only slight snag is that the object isnt destroyed until you delete it

Normal variables exist in the data segment of your program - they are freed up automatically when the program exits. dynamically allocated memory exists on the heap - it isnt in the data segment so it cannot get freed automatically on exit. You need to free it yourself. OS''s have ways of dealing with memory leaks, but they dont always work - you really should delete them yourself rather than rely on the OS to do it for you.
Okay Great thanks, just a few more questions. First what is the data segment and the heap, and what are there differences? Also can u only use the delete operator on pointers?

Thanks,
Chris
when you run an exe, it allocates space for all the variables declared in that exe. The chunk of memory it uses for this program data is called the data segment of your code.

Now, obviously dynamically allocated stuff cant be created in the data segment - the OS has no way of knowing how much space this is going to take up when your program starts. So your dynamically allocated stuff has to go somewhere else - namely the heap. The heap is just the big chunk of free memory that hasnt been assigned elsewhere.
Note that carelessly allocating and deallocating stuff can cause memory fragmentation... which is why it is best to delete stuff in reverse order of allocation if at all possible.

You can only call delete on pointers. non-pointer objects always exist in the data segment.

Edited by - Sandman on July 20, 2001 11:47:28 AM
A simple way to look at it is the data segment is the memory space that your executable occupies, while the heap is the rest of your memory. This is not ''exactly'' true but I don''t think details of the low memory area segmentation is relevant here Let''s just say that it is the area where your hard coded variables are store (your ints, your doubles, your pointer variables, etc). Normally it is faster to access this memory than it is to access memory from the heap. So allocating just for the sake of allocating is counterproductive.

The OS normally manages the heap. When you allocate memory with new, you are making a request for the OS to assign a chunk of memory for your use and your use only. The pointer is an address in memory space that points to the beginning of this chunk of memory. It is also used as an ID for underlying memory manager. So when you delete it, underneath the code is passing this pointer back to the operating system so that it knows you are done using this chunk of memory and it releases it to be used by other processes.

Note also that you can use a pointer to reference memory in the data segment as well. But you do not want to use delete on those pointers. Any hard-coded variables must be released in a different way, normally by the executable stub. Those you don''t need to worry about.

Hope this makes some sense

Seeya
Krippy
Thanks guys that does clear it up completly, I did not know windows used the "heap" memory. Just one more question how much memory is in the heap?

Thanks so much,
Chris
How much memory is in the heap depends on the system.

As far as deleting the pointer though....
Keep in mind that deleting a pointer doesn''t always free all the memory you have used. Take this code...

int *ptr;
ptr = new int[1000];

delete ptr;

The pointer is deleted, but not the 4000 bytes allocated for the integer array. That''s why you need a destructor for your objects that deletes the array...

delete [] ptr;


This topic is closed to new replies.

Advertisement