Sign in to follow this  

Free Store Memory, when?

This topic is 4280 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4280 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this