Sign in to follow this  

[C++]What should I store on the heap?

This topic is 3498 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

I'm seeing in my C++ that the author sometimes stores string objects on the heap, sometimes integer variables and sometimes not. So, what should I do? Store every variable of a class on the heap?

Share this post


Link to post
Share on other sites
Only use the heap for very large objects or when you have to (very large = hundreds of kilobytes).

Storing variables and objects on the stack is faster and you don't have to deal with cleaning them up again before you leave a function.

Share this post


Link to post
Share on other sites
Quick answer: When any of the following is true:
1. The object is pretty big (More than a few KB)
2. You need to allocate a number of objects that you don't know at compile time
3. You need the object to hang around after the current function returns

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quick answer: When any of the following is true:
2. You need to allocate a number of objects that you don't know at compile time
3. You need the object to hang around after the current function returns


I don't understand 2 and 3.

Share this post


Link to post
Share on other sites
Example of case #2:
int size;
int *array;

cin << size; //the value of size is unknown at compile time...
//int error[size] //so we can't do this.
array = new int[size]; //but we can still allocate an array with appropriate size on the heap





Example of case #3:
-snip-

Case #2 typically occurs very often - basically any time you can't tell in advance exactly how much memory your program is going to use. The third case typically occurs when you want to pass messages or objects between other objects, or when you want to add an object to another object without having it destroyed when the function retutns.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hnefi
Example of case #2:
*** Source Snippet Removed ***

Example of case #3:
*** Source Snippet Removed ***

Case #2 typically occurs very often - basically any time you can't tell in advance exactly how much memory your program is going to use. The third case typically occurs when you want to pass messages or objects between other objects, or when you want to add an object to another object without having it destroyed when the function retutns.
Case #3 is a memory leak [smile]
You probably want:

void MakeObject(Object **obj)
{
*obj = new Object();
}

// Or:
Object* MakeObject()
{
return new Object();
}

Share this post


Link to post
Share on other sites
Because the function copied the pointer, then allocated a new instance to that copy. When the function returned, the copy went out of scope and the created instance was lost.

Share this post


Link to post
Share on other sites

This topic is 3498 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