Thread: What is happening with this code??

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    19

    What is happening with this code??

    I have this function that is meant to draw the highscores using a class called fileHandler which manages the highscore.txt file.

    For some reason which I cannot see right now, when I step through the code I notice that the values for scoreList (which contain a structure called scoreData that consist of a string and a int for the score) are changing in the introduction for the for loop.

    It goes from having a empty string and a integer holding 6 to a really small integer, like when a value is uninitialized, and a string with a bad pointer.

    How is this being altered?????

    Code:
    void menu::drawScoreBoard(void)
    {
    ///////////////////////////DRAW THE SCORE BOARD HERE//////////////////////////////////////////////////////////////////
        fileHandler scoreFile;
        std::vector <dataType::scoreData *> scoreList;
        std::string temp;
        int count = 1;
    
        if (scoreFile.getScore(&scoreList))
        {
            for (std::vector<dataType::scoreData *>::iterator it = scoreList.begin(); it < scoreList.end(); it++) //error here
            {
                temp = drawArea->IntToStr(count) + ") " + (*it)->initials + drawArea->IntToStr((*it)->score);
                drawArea->drawMessage(temp, scoreBoardPos.x, scoreBoardPos.y +count);
                count++;
            }
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
        std::vector <dataType::scoreData *> scoreList;
        std::string temp;
        int count = 1;
     
        if (scoreFile.getScore(&scoreList))
    Is this just filling in a vector with a load of pointers to memory that no longer exists (say local vars in getScore()) ?

    Forget the pointers and trying to make it "efficient", and get it working by making a regular copy of the data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Not to mention, if you're populating a vector, then the method name getScore() is confusing, because it's actually getting a list of scores, as the variable itself states clearly. Better to make that method getScores(std::vector<dataType::scoreData *> &scoreList) or getScoreList(std:vectordataType::scoreData *> &scoreList)

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    I forgot the pointers like you said and it worked like a treat, thanks!

    @rags_to_riches Noted, I have been slightly changing functions around without changing the name.

    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why is this happening?!
    By MK27 in forum C Programming
    Replies: 3
    Last Post: 01-13-2009, 12:40 PM
  2. Why does this keep happening?
    By darknite135 in forum C++ Programming
    Replies: 5
    Last Post: 12-23-2007, 12:20 PM
  3. Why is this happening?
    By caduardo21 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2005, 01:09 AM
  4. This can't be happening
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-28-2003, 04:00 PM
  5. Don't know why this is happening
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-27-2002, 06:30 PM