Thread: Stack and Free Store Objects

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Stack and Free Store Objects

    I'm a Java programer trying to complete a project in c++.
    I want to create a vector as a member of a class.

    Code:
    class SpriteManager{
         vector<Sprite*> spriteList;
    };
    Why won't this work? It will not compile if I try to use "spriteList" in my class's scope. I've tried having a pointer to a vector and using the "new" operator, but the code gets ugly with all the indirections.

    Also, I've noticed that tutorials always show how to make an object this way.

    Code:
    ObjectType myObject;
    Then they mention that you can have a pointer to an object. I'm used to having pointers to objects but why would the language allow an object to reside on the stack? Is there an advantage?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    What's the error ?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    You know, I just messed around with it and I got it to work. My second question still stands.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Memory allocated on the stack has it's size predetermined at compile-time, and can not be changed. When it goes out of scope, as from a function, it is automatically 'deallocated'. Memory allocated on the heap can be of any specified size, however you need to keep track of a pointer to it and deallocate it yourself. So in a class like this, to add a Sprite to the vector, you might say

    Code:
    spriteList.add(new Sprite(blah blah blah));
    Then, in the destructor of the class, you would iterate through the vector, and delete all the entries you made in the vector. Every call to new needs a corresponding call to delete. You would (probably) not want to store pointers to objects on the stack in your vector because the Sprite might go out of scope even if your spriteManager persisted.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    So a in this case a pointer to a vector would be better? (I think so)

    With the code as is, my vector resides on the stack?

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I would just use a regular Sprite type without a pointer.
    Woop?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Frank_Li
    So a in this case a pointer to a vector would be better? (I think so)

    With the code as is, my vector resides on the stack?
    Your vector is a member of the class so it resides where ever the instance of your class resides. A pointer to a vector is unnecercary in this case since it's size is predictable (not the actual size used by it's contents the instance of the vector).

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> So a in this case a pointer to a vector would be better? (I think so)
    No. In general, don't use pointers unless you need to. The stack versus heap difference is only part of the issue, since a member variable will reside wherever the instance of the class resides.

    A more important reason to prefer to use a regular instance is that it is cleaned up automatically. In Java, the garbage collector cleans up the memory for you, but in C++, there is no garbage collector. Anything that you allocate with new you must deallocate with delete. That can be error prone, and so it is often better to let the compiler arrange for creation and destruction for you.

    Some cases in which you might want to use a pointer are when you are holding subclass objects together. For example, if your Sprite class is a superclass and there are different subclasses that have been defined, then your vector should be vector<Sprite*> as you have it so that you can store the pointers to the superclass and use polymorphism on those objects. Another case is when your object uses a lot of memory that is too big for the stack or needs to be allocated dynamically. An example of this is the vector class itself, which usually does use a pointer internally to allocate space for its data. One more case where you might consider a pointer is if the lifetime of the object will be longer than the scope it is created in.

    None of those cases apply to the vector, so you should not make it a pointer.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Thanks for the discussion Daved, that helped alot. Does any one know where I can find more info on this. All the tutorials I find never mention polymorphisim, or when a pointer should be used.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's really the kind of stuff you learn from a book, from experience, or from reading and asking questions like this. Unfortunately many books available teach C++ from a C perspective, and pointers are used much more extensively in C.

    If you consider yourself to be a competent enough programmer in Java that you don't really need a beginning C++ book, I would recommend C++ Coding Standards as your guide to best practices in C++. In refreshing my Java knowledge I found myself wishing such a book existed for that language. If you are still fairly new to programming or want some extra help on C++, then Accelerated C++ is your answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and the free store
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2005, 04:23 PM
  2. Finished Stack
    By the pooper in forum C Programming
    Replies: 11
    Last Post: 02-02-2005, 10:52 AM
  3. Stacks
    By Cmuppet in forum C Programming
    Replies: 19
    Last Post: 10-13-2004, 02:32 PM
  4. HEap and stack, I'm confused
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-31-2002, 10:59 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM