Jump to content
  • Advertisement
Sign in to follow this  
Tradone

Ever Since I learned pointers

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

Ever since I learned how to use pointers, I declare all my variables in the heap, rather in the stack. Is this generally a good idea? I mean, what would be the rule of thumb to decide whether I should declare in the heap rather than the stack? and there seems to be a difference between the free store and the heap. I thought there were the same things.

Share this post


Link to post
Share on other sites
Advertisement
normally things on the heap (ie. the use of pointers) occur when you are moving large variables (such as a class or struct) through functions or when you need to create an object at runtime (ie. in the middle of running your program) and it will be removed, moved, or destroyed during runtime.

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
normally things on the heap (ie. the use of pointers) occur when you are moving large variables (such as a class or struct) through functions or when you need to create an object at runtime (ie. in the middle of running your program) and it will be removed, moved, or destroyed during runtime.


Then I would safely assume that most big/modern projects will most definately use heap memory.

Share this post


Link to post
Share on other sites
anything that does not need to remain after the end of the scope, and which is not humongous in size should be put on the stack.

it is much more expensive to call new / delete than it is to put something on the stack (which has zero overhead).

Share this post


Link to post
Share on other sites
Quote:
Original post by Tradone
Quote:
Original post by Alpha_ProgDes
normally things on the heap (ie. the use of pointers) occur when you are moving large variables (such as a class or struct) through functions or when you need to create an object at runtime (ie. in the middle of running your program) and it will be removed, moved, or destroyed during runtime.


Then I would safely assume that most big/modern projects will most definately use heap memory.

You assume very correctly, but not everything is put on the heap. So don't go pointer crazy [smile]. Safest thing at this point is to make some code that works, post it on the boards, and let one of the senior members (ie. not me [smile]) point out improvements or rip it to shreds [grin]

Share this post


Link to post
Share on other sites
Not for everything...

int* i = new int(27);

try{
while(*i--)
do_stuff();
}catch(...){
delete i;
throw;
}

delete i;

would be a bit silly compared to

int i=27;
while(i--)
do_stuff();

. [wink]

Share this post


Link to post
Share on other sites
General rules of thumb - use the stack when you can, use the heap/freestore when you must and always wrap pointers in stack allocated RAII wrappers (i.e. std::vector implicitly wraps operator new[], std::auto_ptr and boost::shared_ptr explicitly wrap operator new).

Σnigma

Share this post


Link to post
Share on other sites
Thank you for all the replies.

Well, I have a class, Config as the parent class and have Path and Data as the child. But it seems like Path doesn't necessarily need to be declared it in the heap.

However if I declare Path on the stack I would have to call member functions like this : pathInstance.Get(); while dataInstance->Get();

and just makes things hell lot confusing.
and wanted some advice...
Sorry I should have explained this scenario in advance.

Edit:
As Enigma suggests, I should declare path on the stack, but it may be harder to maintain. ( I mean it's not that hard ). So I guess this is where

codemaintainability vs. performance

is an issue?

Share this post


Link to post
Share on other sites
Quote:
Original post by Tradone
However if I declare Path on the stack I would have to call member functions like this : pathInstance.Get(); while dataInstance->Get();
Using stack vs heap makes no difference whether you have to call member functions or not. In fact it can make it simpler on the stack because you can use . instead of ->

Share this post


Link to post
Share on other sites
Quote:
Original post by MrEvil
Not for everything...

int* i = new int(27);

try{
while(*i--)
do_stuff();
}catch(...){
delete i;
throw;
}

delete i;

would be a bit silly compared to

int i=27;
while(i--)
do_stuff();

. [wink]


It'd be silly compared to:

std::auto_ptr< int > i ( new int( 27 ) );
while ((*i)--)
do_stuff();


As well. But at least this version is only silly compared to the stack version with regards to the needless heap-allocation of the variable.

OP: Prefer the stack unless you have something long lived, or the API you're using gives you a pointer to work with. If you havn't allready, learn about smart pointers - they can automate a bunch of repetitious code (see the first quoted code block above) into something far more managable. I highly recommend the Boost C++ Libraries, specifically the Boost Smart Pointers Library for this discussion. Might want to check out Loki as well.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!