Free Store Memory, when?

Started by
5 comments, last by simon10k 18 years ago
When do you know to use the memory in the free store rather just declare a bound object or just a declared object? what's the difference? edit) and what's the deal with setting the pointer to null before deleting the pointer? edit2) -> okay they're due to straying pointers and nulling them out dismantles a potential time bomb, is what i read.
Advertisement
Quote:Original post by Tradone
When do you know to use the memory in the free store rather just declare a bound object or just a declared object?

what's the difference?

Not sure what you're asking. Are you asking when to allocate on the heap and when to allocate on the stack?

Quote:
edit) and what's the deal with setting the pointer to null before deleting the pointer?


You should not set a pointer to NULL before deleting it. The memory it was pointing to will be lost, and it will create a memory leak. It is, however, a good idea to set it to NULL after deleting it. Deleting a pointer that was already deleted will almost always cause a crash, while deleting NULL will do nothing.
Quote:Original post by Roboguy
Quote:Original post by Tradone
When do you know to use the memory in the free store rather just declare a bound object or just a declared object?

what's the difference?

Not sure what you're asking. Are you asking when to allocate on the heap and when to allocate on the stack?


right that's what I'm asking.
but it seems like I don't really even know what a heap / stack / or a queue is .

Quote:
edit) and what's the deal with setting the pointer to null before deleting the pointer?

You should not set a pointer to NULL before deleting it. The memory it was pointing to will be lost, and it will create a memory leak. It is, however, a good idea to set it to NULL after deleting it. Deleting a pointer that was already deleted will almost always cause a crash, while deleting NULL will do nothing.

ah.. I see.. but why would it create a memory leak when the data inside that memory can be easily overridden?
sorry about that I found the answers,

Q. Why are pointers so important?

A. Today you saw how pointers are used to hold the address of objects on the free store and how they are used to pass arguments by reference. In addition, on Day 13, "Polymorphism," you'll see how pointers are used in class polymorphism.

Q. Why should I bother to declare anything on the free store?

A. Objects on the free store persist after the return of a function. Additionally, the ability to store objects on the free store enables you to decide at runtime how many objects you need, instead of having to declare this in advance. This is explored in greater depth tomorrow.

Q. Why should I declare an object const if it limits what I can do with it?

A. As a programmer, you want to enlist the compiler in helping you find bugs. One serious bug that is difficult to find is a function that changes an object in ways that aren't obvious to the calling function. Declaring an object const prevents such changes.

to some of my questions. :D
Quote:
but why would it create a memory leak when the data inside that memory can be easily overridden?

if its allocated using the new keyword, or malloc() it is allocated on a heap segment by the heap implementation used by your compiler. Now if you NULL a pointer the heap implementation that is still holding the data that the pointer refrenced will continue to hold that data, and will do so until delete or free() is called respectively. However, now that the pointer is NULL it is practically impossible to erase/remove that data from its heap segment. So overwriting that data is not going to happen, and the data will just sit and take up memory until your program quits, hence a memory leak
_____________________#define ever (;;)for ever delete (void *) rand();
i see.
Thanks for the info!!

and another question: the STL classes uses free store memory right?
or do I have to make them use free store memory?
This is to the best of my knowledge...

Your asking why should you use the free store instead of using automatic variables.

#1
If you dont know what size an array needs to be at compile time sometimes it is better to dynamically allocate the memory.

#2
Also, the lifetime of an automatic variable is determined by its scope. if you need an object (or objects) to exist independanly of scope you can allocate them on the free store

STL types behaive like the built in types.
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003

This topic is closed to new replies.

Advertisement