Should I use the Heap or the Stack?

This is a discussion on Should I use the Heap or the Stack? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm creating some objects which come from various inherited classes (with a single base class). After the objects have ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Should I use the Heap or the Stack?

    Hi,

    I'm creating some objects which come from various inherited classes (with a single base class).

    After the objects have been created, a want to save the object's values to a text file, bit like a database.

    Up until now I have always used this type of constructor:

    Code:
    foo node(arg1, arg2);//old constructor for the stack
    I have come to learn that this creates on object on the stack.

    But after reading how references give more functionality, I think they would be better for my requirements.

    I was hoping for some general advice on whether this would be the right decision.

    I have managed to create a pointer to the objects, using this:

    Code:
    foo *ptr = new foo(arg1, arg2);
    Which I believe creates the object in the heap and the pointer on the stack.

    Do you think I should use references?

    I have a vector which which I need to use to store either pointers or references to the mixed class objects.

    Right now it looks like this:

    Code:
    vector <foo*> vectorname;

    So basically I'm wondering if I should use references, or stick with pointers?

    Many thanks for any advice!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A reference must refer to something -- i.e., you must have a node hanging out before you can have a reference to said node. It is not something you can have instead of creating objects.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    If you're storing a vector of these objects, and you want to store objects from all derived classes in the vector, then your vector should hold base class pointers that point to objects allocated with new. This way you can take advantage of polymorphism. There are different solutions on how to best manage the memory if you do this, including ptr_vector or vector<shared_ptr<base_class> >, but even a vector<base_class*> can be relatively easy to implement.

    In general, you should prefer objects on the stack, but the above is one exception to this rule.

    If you have vectors that will only have instances of a single derived class at a time, then vector<derived_A> and vector<derived_B> might be best, because you wouldn't need the pointer to use polymorphism.

    Finally, note that you use the term reference in your post, but are talking mostly about pointers. C++ has a reference object, so using that terminology can be confusing. If you're talking about pointers, say pointers to be more clear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 04:03 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21