Thread: Stack or Heap Private Data...?

  1. #1
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    Arrow Stack or Heap Private Data...?

    I'm making a text-RPG. I have a Room class and Monster class (I'm sure you can tell what they are for.)

    I would like a Monster object in every Room object as a private data member. My question is, should this be on the free store or just a regular member on the stack?

    Code:
    ...
    private:
      Monster mob;
    or
    Code:
    ...
    private:
      Monster * mob;
    What are the advantages of using each? Am I being to vague*?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you have exactly one monster, then
    private:
    Monster mob;
    may be OK
    But you also need
    bool monsterIsAlive;

    If you have
    private:
    Monster * mob;
    Then you can 'kill' the monster with
    delete mob; mob = NULL;

    Perhaps a list of monsters (possibly empty) in each room would be better

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by Salem
    Perhaps a list of monsters (possibly empty) in each room would be better
    That was my first thought. You are limiting your code by saying maximum one monster per room

  4. #4
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Good idea. i'm not quite sure how to have a list of monsters in the room, though. all the rooms will be stored in an array on the heap, so they'll all have the same numbers/types of private data... how can i vary the number and types of monsters in each room? *cries*

    oh, i have another quetions that is related to that.. let's say I have a the Room array on the heap, and i delclare a private data member .. is that private data member on the heap?? what if i make that private member a pointer to a heap variable...isn't that doing the same hting?

    i suck at explaining!
    Last edited by GrNxxDaY; 08-09-2002 at 02:05 AM.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > how can i vary the number and types of monsters in each room?
    Well I would assume the type of the monster is stored somewhere inside the Monster class.

    As for the number, you could try one of these

    1. Simple array, allows a max number of monsters
    Monster monsters[10];

    Simple to do, but limits the max you can have, and perhaps wastes a lot of space.

    2. A dynamic array, which you allocate using new
    Monster *monsters;
    monsters = new Monsters[10]; // or the number you need

    Saves on the space compared to method 1, but care is needed when expanding and shrinking the array

    3. A traditional linked list
    Do a board search for linked lists

    4. Use an STL container such as <list> or <vector>
    I don't know much more than that

    > is that private data member on the heap??
    Yes
    The privacy just tells you who is allowed to access that member of the class (not where it is stored).

    > what if i make that private member a pointer to a heap variable...isn't that doing the same hting?
    Mostly, though it might not be as private as you think. If you mistakenly return that pointer to a function outside the class, your data is no longer private.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help with Stack
    By trongsi in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2006, 04:14 PM
  2. webBrowser problem
    By algi in forum C# Programming
    Replies: 7
    Last Post: 08-19-2005, 09:35 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Accessing data in the program stack
    By dajur in forum C Programming
    Replies: 1
    Last Post: 09-23-2001, 10:15 PM