Ever Since I learned pointers

Started by
20 comments, last by Tradone 18 years ago
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.
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.

Beginner in Game Development?  Read here. And read here.

 

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.
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).
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]

Beginner in Game Development?  Read here. And read here.

 

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]
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
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?
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 ->
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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.

This topic is closed to new replies.

Advertisement