Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointer Problem - I think

Started by siliconsurfer Feb 24, 2005 at 8:37 AM 0 replies 450+ views
Original Post
siliconsurfer
siliconsurfer
Hi I'm having problems, which I think are down to pointers. I'm trying to read some values from a text file, store them in a vector, and then in another method read them from the vector. First I have a class called orc with the following attributes:-

        private:
                char *mDirection; //The current direction the sprite is facing
                int mDistance; //How for the sprite has moved
                int mCurrentPos; //Position to read from in array
                int mLife;
                vector<char *> mtheDirection;
                vector<int> mtheDistance;

Below is the first method in the orc clas it should read the orc.data file and read in a direction which is W, E, N or S and a distance which could be 0-999. In this method I printed out the contents of the vector after asigning it, and it prints the values just fine.

int orc::loadDirections()
{
        FILE *fp;
        char *filename="data/orc.data";
        char buffer[255];
        char dir;
        int dis;
        int count = 0;

        if((fp=fopen(filename, "r")) == NULL)
        {
                printf("Error opening file %s\n", filename);
                return -1;
        }
        while(!feof(fp))
        {
                fgets(buffer, 255, fp);
                sscanf(buffer, "MV: %s %d", &dir, &dis);

                mtheDirection.push_back(&dir);
                mtheDistance.push_back(dis);
                printf("DIR: %s\n", mtheDirection[count]);
                printf("DIS: %d\n", mtheDistance[count]);
                printf("COUNT %d\n", count);
                count++;
        }
        mCurrentPos = 0; //Current Posistion in the array
        mDistance =0; //Distance moved so far

        mtheDirection.push_back("W");
        mtheDirection.push_back("S");
        mtheDistance.push_back(200);
        mtheDistance.push_back(200);

        return 0;
}

Next I have this method in Orc it begins by assigning the contents of the first part of a vector to mDirection. (mCurrentPos is set to 0 in the previous method). My problem is that if I do a printf("mDirection") it prints out garbage. From what I can see it's not assigning the contents of the vector, but some other value, I can't understand why it does this, when in the previous method (both methods of orc) that sort of assignment works fine. Any ideas?

void orc::patrol()
{
        mDirection = mtheDirection[mCurrentPos]; //Set the direction to what it should be
        setDirection();

        if(mDistance > mtheDistance[mCurrentPos]) //If the object has walked the full distance we increment the
                                                  //position in the array
        {
                if(mCurrentPos < mtheDirection.size()-1)  //size of the array
                {
                        mCurrentPos++;
                }else{
                        mCurrentPos=0;
                }
                mDirection = mtheDirection[mCurrentPos];
                mDistance = 0;

        }else{                                  //Else we move the object in the correct direction
                if(strcmp(mDirection, "N") == 0)
                {
                        mY--;
                }else if(strcmp(mDirection, "S") == 0)
                {
                        mY++;
                }else if(strcmp(mDirection, "W") == 0)
                {
                        mX--;
                }else if(strcmp(mDirection, "E") == 0)
                {
                        mX++;
                }
                mDistance++;  //Increment the distance the object has moved
        }
}

Cauthon
Cauthon
You're storing a pointer to a 'char' in your vector. These pointers point at a block of memory. The memory that you're pointing to is created on the stack for the while-loop and when the while-loop exits your memory and pointers are no longer valid. You should also get garbage in your second code block if you try and print out the results of your vectors after the while-loop completes, because as soon as you pass the close brace your memory is gone. (actually, the memory is allocated at the top of the function, so all the values in your vector will point to the last value stored in the while loop. They won't be invalid until you pass the function's close brace)

You should create a vector of 'char's instead of 'char *'s. When you push a char onto the vector it will create a copy of it and the data will be maintained. In order to do this, you'll need to use single quotes instead of double quotes in your switch statement so that you get the character representation and not the string.

I believe that you should make it look something like this:
private:   char mDirection; //The current direction the sprite is facing   int mDistance; //How for the sprite has moved   int mCurrentPos; //Position to read from in array   int mLife;   vector<char> mtheDirection;   vector<int> mtheDistance;


Then

int orc::loadDirections(){   FILE *fp;   const char *filename="data/orc.data";   char buffer[255];   char dir;   int dis;   int count = 0;   if((fp=fopen(filename, "r")) == NULL)   {      printf("Error opening file %s\n", filename);      return -1;   }   while(!feof(fp))   {      fgets(buffer, 255, fp);      sscanf(buffer, "MV: %c %d", &dir, &dis);      mtheDirection.push_back(dir);      mtheDistance.push_back(dis);      printf("DIR: %c\n", mtheDirection[count]);      printf("DIS: %d\n", mtheDistance[count]);      printf("COUNT %d\n", count);      count++;   }   mCurrentPos = 0; //Current Posistion in the array   mDistance =0; //Distance moved so far   mtheDirection.push_back('W');   mtheDirection.push_back('S');   mtheDistance.push_back(200);   mtheDistance.push_back(200);   return 0;}


and finally
void orc::patrol(){   mDirection = mtheDirection[mCurrentPos]; //Set the direction to what it should be   setDirection();   if(mDistance > mtheDistance[mCurrentPos]) //If the object has walked the full distance we increment the                                                  //position in the array   {      if(mCurrentPos < mtheDirection.size()-1)  //size of the array      {         mCurrentPos++;      }else{         mCurrentPos=0;      }      mDirection = mtheDirection[mCurrentPos];      mDistance = 0;   }else{                                  //Else we move the object in the correct direction      if(strcmp(mDirection, 'N') == 0)      {         mY--;      }else if(strcmp(mDirection, 'S') == 0)      {         mY++;      }else if(strcmp(mDirection, 'W') == 0)      {         mX--;      }else if(strcmp(mDirection, 'E') == 0)      {         mX++;      }      mDistance++;  //Increment the distance the object has moved   }}


You could alternatively use new/delete to create memory on the heap which will not be tied to a particular scope. But that is a lot more trouble that it's worth if you're just trying to store single characters.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.