This topic is now archived and is closed to further replies.


Pointers, memory and trouble, need help...

Recommended Posts

Well, tonight i decided to write a "simple" game. 3 hours later my simple game has gotten very cryptic. Basically MonsterList should be a dynamic created list of Monsters. It uses the stream operator to add items and the [] operator to access them. I basically had to do this because my game now will now have a create-a-monster feature that will hold an unlimited amount of monsters, hardware will, of coarse. Anyway, I was wondering if somone could tell me whats going on. It compiles but just seems filled with random numbers after I add a monster.

class Monster{

   	int Int, Str, Agi;
      // Inteligence, Strength and Agility
      int Elem;
      // Element
      int Class;
      // Mage, Warrior or Thief
      Bar Mana, Edu, Hp;
      // Mana, Endurance, Hit points
      Pic Glyph[3];
      // Graphic, 3 faces

class MonsterList{
   	Monster *Data;// stores all the data
      int items;// keeps a running track of the items in the list

      Monster operator[](int);
      void operator<<(Monster);

	Data = (Monster*)malloc(0);
   items = 0;

Monster MonsterList::operator[](int x){
	return Data[x];

void MonsterList::operator<<(Monster x){

	Monster *Temp;

   items ++;  // add an item to list
   Temp = (Monster*)malloc(sizeof(Monster)*items);
   // Makes a temp array the size of the monsters we will need

   memcpy((void*)Data, (void*)Temp, sizeof(Monster)*(items-1));
   // copies all the current data into temp

   delete [] Data; // deletes all the information in data

   Monster *toPoint = Temp;

   for(int t=0; t<(items-1); t++){  // advances the pointer to where I need the new monster

   memcpy((void*)&x, toPoint, sizeof(Monster));
   // Takes x and adds it to toPoint, which should point to the last monster space
   // in Data

   Data = Temp;

Share this post

Link to post
Share on other sites
rather than writing your own linked list, just use the stl vector class. it's way easy, and then all you have to do is create your monster object and just add monsters to the vector.

stl vector class docs:

to use it for your specific case:

vector<Monster *> monsters;

then make a monster object and use the push_back() method:

//pMonsterObject is : Monster * pMonsterObject

you can access it like a normal array with square brackets (can't get them to show up here....)

and you can figure out the length of the list with:



you can read the other ongoing thread about the STL happening "right now" on gamedev forums:


Edited by - Palidine on January 31, 2002 8:07:38 PM

Share this post

Link to post
Share on other sites
Am I doing this right?

#include <iostream.h>
#include <vector.h>

void main(){

vector V;


// btw, you can make square brakets by putting them in between code and /code blocks.

It wouln''t compile for me, undefined symbol vector. My compiler has vector.h though and I even opened and found the class. Not sure what I''m doing wrong, haven''t done much with templates.

Share this post

Link to post
Share on other sites
The .h versions of those headers are the old-style and are considered obsolite. Instead, use the versions without the .h. In other words:
#include <iostream>
#include <vector>

// this is required, otherwise you''ll have to prefix everything

// from the above headers with std::

using namespace std;

There''s one other thing. vector is great, but will reallocate itself every time you add an item. Therefore, if you know exactly or approximately how many items you will be adding (or needing in total), use vector''s reserve function to tell the object to reserve the specified amount of memory so subsequent additions won''t require reallocation until the reserved memory is exhaused. Not only is this better for keeping the memory clean, but it substantially improves performance. If you have absolutely no idea what you''ll be adding, then condider reserving x slots for every first-in-x additions.

Share this post

Link to post
Share on other sites
Aahhh... don''t mix malloc and delete. Either use malloc and free, or new and delete. In C++, you should nearly always use new and delete. The rest of your code was a very awkward mix of old-style C and new-style C++. You shouldn''t copy classes with memcpy, you should copy them using a copy constructor or assignment operator, both of which will probably just contain a lot of assignments themselves. It could also be considered bad style to use the << operator to add something to a list, but that''s just an opinion.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post

Link to post
Share on other sites
just make sure when using vectors that you do them like:

vector<Monsters *> v;

(i.e. a vector of pointers to object monster)

instead of just:

vector v;


Edited by - Palidine on February 1, 2002 1:25:40 AM

Share this post

Link to post
Share on other sites