Another crash bug?

This is a discussion on Another crash bug? within the C++ Programming forums, part of the General Programming Boards category; {WinXP, Borland C++ free command line) I havent had this problem in awhile. My program has started crashing when it ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Another crash bug?

    {WinXP, Borland C++ free command line)
    I havent had this problem in awhile. My program has started crashing when it runs. No compiler errors, so it must be a logic error. I have isolated the problematic code;

    The used class:
    Code:
    #include "KeeperClass.h"
    
    #define Enemy1 '@'
    #define Enemy2 '&' 
    #define Enemy3 '$'
    #define Enemy4 '%'
    
    #define E_Left 10 //Movement Definitions.
    #define E_Right 20
    #define E_Up 30
    #define E_Down 40
    
    class Enemy {
     private:
    
     char Ent;
     int ex_pos;
     int ey_pos;
     int tag;
     int mproc;
     SHORT alive;
    
     public:
    
     Enemy() {Ent=0;ex_pos=0;ey_pos=0;tag=0;mproc=0;alive=0;}
    
     Enemy(char t, int ex, int ey, int etag, int moving, SHORT live) {Ent=t; ex_pos=ex;ey_pos=ey;tag=etag;mproc=moving;alive=live;}
    
     COORD GetPos() {COORD m; m.X=ex_pos; m.Y=ey_pos; return m;}
     char GetType() {return Ent;}
     int GetX() {return ex_pos;}
     int GetY() {return ey_pos;}
     int GetTag() {return tag;}
     int GetMovement() {return mproc;}
     SHORT IsAlive() {return alive;}
     SHORT operator==(Enemy X);
     SHORT IsNull();
    
     void SetType(char c) {Ent=c;}
     void SetX(int x) {ex_pos=x;}
     void SetY(int y) {ey_pos=y;}
     void SetTag(int Tag) {tag=Tag;}
     void SetMovement(int Move) {mproc=Move;}
     void SetLive(int live) {alive=live;}
     void Delete();
    };
    
    SHORT Enemy::operator==(Enemy X) {
     if(this->GetType() == X.GetType() && this->GetX() == X.GetX() && this->GetY() == X.GetY() && this->GetTag() == X.GetTag())
      return 1;
    
     return 0;
    }
    
    SHORT Enemy::IsNull() {
     Enemy Null(0, 0, 0, -1, 0, 0);
    
     if(*this == Null)
      return 1;
    
     return 0;
    }
    
    void Enemy::Delete() {
     COORD m;
    
     m.X = this->GetX();
     m.Y = this->GetY();
     SetConsoleCursorPosition(hOt, m);
     cout << ' ';
    }
    The crash started when I changed this line:
    Code:
     Enemy(char t, int ex, int ey, int etag, int moving, SHORT live) {Ent=t; ex_pos=ex;ey_pos=ey;tag=etag;mproc=moving;alive=live;}
    It used to be backwards (ex = ex_pos) it was a very strange mistake. Even stranger, when fixed, it caused the crashs to start.

    And my problematic code:

    Code:
    void MoveEnemies() {
     unsigned int x;
     COORD xy;
     SHORT d;
    
     if(Ens.empty())
      return;
    
     for(x=0; x < Ens.size(); x++) {
      Ens[x].SetMovement(E_Left);
      xy=Ens[x].GetPos();
      d=Ens[x].GetMovement();
    
      if(d == E_Right) {
       SetPos(xy.X, xy.Y, 0, 0);
       cout << Blank;
       SetPos(xy.X, xy.Y, 1, 0);
       Ens[x].SetX(xy.X+1);
       SetColor('r');
       cout << Ens[x].GetType();
       SetColor('o');
      } else
       if(d == E_Left) {
        SetPos(xy.X, xy.Y, 0, 0);
        cout << Blank;
        SetPos(xy.X, xy.Y, -1, 0);
        Ens[x].SetX(xy.X-1);
        SetColor('r');
        cout << Ens[x].GetType();
        SetColor('o');
       }
     }
    }
    
    void EvaluateEnemies() {
     unsigned int x, y;
     COORD xy;
     char Yam[2][1];
    
     if(Ens.empty())
      return;
    
     for(x=0; x < Ens.size(); x++) {
      xy=Ens[x].GetPos();
      Yam[0][0]=ReadPos(xy.X, xy.Y, 1, 1);
      Yam[1][0]=ReadPos(xy.X, xy.Y, -1, 1);
      if(Yam[0][0] == Blank && Yam[1][0] == Blank)
       Ens[x].SetMovement(0);
      if(Yam[0][0] == Blank)
       Ens[x].SetMovement(E_Left);
      if(Yam[1][0] == Blank)
       Ens[x].SetMovement(E_Right);
     }
    }
    
    int EnemyMain() {
     EvaluateEnemies();
    
     MoveEnemies();
    
     return 0;
    }
    Both MoveEnemies() and EvaluateEnemies() cause problems. None of my other calls to enemy functions cause problems.

    |Also| Ens is a global enemy vector.
    Code:
    vector <Enemy> Ens;
    I have a feeling this isint my only problem with my code. Something very strange is going on with the enemy class.

    When the call to MoveEnemies() was removed, the crashing stopped, so I'm assuming thats the issue. If this has to many holes, I can upload the whole source. (Its not that big :P)
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    use an iterator instead of array indexing -- array indexing will crash if an elements has been erased, while iterators skip over erased elements of the array

    Also, in the code you posed, Yam does not need to be a 2d array, simple 1d array will do the job.

    Code:
    void EvaluateEnemies() {
     unsigned int x, y;
     COORD xy;
     char Yam[2];
    
     if(Ens.empty())
      return;
     vector<Enemy>::iterator it;
     for(it = Ens.begin(); it != Ens.end(); it++)
     {
        Enemy& en = *it;
      xy=en.GetPos();
      Yam[0]=ReadPos(xy.X, xy.Y, 1, 1);
      Yam[1]=ReadPos(xy.X, xy.Y, -1, 1);
      if(Yam[0] == Blank && Yam[1] == Blank)
       en.SetMovement(0);
      if(Yam[0] == Blank)
       en.SetMovement(E_Left);
      if(Yam[1] == Blank)
       en.SetMovement(E_Right);
     }
    }

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    It still crashs :/. I dont delete array parts when the program launches - so I dont see how my array could be going into bad sectors of memory. Even so, with your code the program still crashs.

    I'm applying an iterator method to MoveEnemies() to see if that helps.

    Thanks!

    [Edit]
    Code:
    void MoveEnemies() {
     vector<Enemy>::iterator it;
     unsigned int x;
     COORD xy;
     SHORT d;
    
     if(Ens.empty())
      return;
    
     for(it = Ens.begin(); it != Ens.end(); it++) {
      Enemy& En = *it;
      En.SetMovement(E_Left);
      xy=En.GetPos();
      d=En.GetMovement();
    
      if(d == E_Right) {
       SetPos(xy.X, xy.Y, 0, 0);
       cout << Blank;
       SetPos(xy.X, xy.Y, 1, 0);
       En.SetX(xy.X+1);
       SetColor('r');
       cout << En.GetType();
       SetColor('o');
      } else
       if(d == E_Left) {
        SetPos(xy.X, xy.Y, 0, 0);
        cout << Blank;
        SetPos(xy.X, xy.Y, -1, 0);
        En.SetX(xy.X-1);
        SetColor('r');
        cout << En.GetType();
        SetColor('o');
       }
     }
    }
    This did not help :/. It still crashs, unfortunantly. Any other ideas?
    Last edited by Blackroot; 02-20-2006 at 03:55 AM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    This bit of code is definantly the problem; it compiles, but it crashs.

    Code:
    void MoveEnemies() {
     vector<Enemy>::iterator it;
     unsigned int x;
     COORD xy;
     SHORT d;
     Enemy En;
    
     if(Ens.empty())
      return;
    
     for(it = Ens.begin(); it != Ens.end(); it++) {
      En = *it;
      En.SetMovement(E_Left);
      xy=En.GetPos();
      d=En.GetMovement();
    
      if(d == E_Right) {
       SetPos(xy.X, xy.Y, 0, 0);
       cout << Blank;
       SetPos(xy.X, xy.Y, 1, 0);
       En.SetX(xy.X+1);
       SetColor('r');
       cout << En.GetType();
       SetColor('o');
      } else
       if(d == E_Left) {
        SetPos(xy.X, xy.Y, 0, 0);
        cout << Blank;
        SetPos(xy.X, xy.Y, -1, 0);
        En.SetX(xy.X-1);
        SetColor('r');
        cout << En.GetType();
        SetColor('o');
       }
     }
    }
    
    void EvaluateEnemies() {
     unsigned int x, y;
     COORD xy;
     char Yam[2];
     vector<Enemy>::iterator it;
     Enemy en;
    
     if(Ens.empty())
      return;
    
     for(it = Ens.begin(); it != Ens.end(); it++) {
      en = *it;
      xy=en.GetPos();
      Yam[0]=ReadPos(xy.X, xy.Y, 1, 1);
      Yam[1]=ReadPos(xy.X, xy.Y, -1, 1);
      if(Yam[0] == Blank && Yam[1] == Blank)
       en.SetMovement(0);
      if(Yam[0] == Blank)
       en.SetMovement(E_Left);
      if(Yam[1] == Blank)
       en.SetMovement(E_Right);
     }
    }
    
    int EnemyMain() {
     cout << '1';
     EvaluateEnemies();
     cout << '2';
     MoveEnemies();
     cout << '3';
    
     return 0;
    }
    It gets to 1. When EvaluateEnemies() is removed, it still crashs. When its put back, and MoveEnemies() is removed, it still crashs. So, I'm guessing it has to be both of them. Calls to enemy function do not seem to cause issues in other functions, so I'm going to guess this is a memory issue. But, if iterators skip deleted memory, how could that be? As far as my experience goes, only going into bad memory has caused crashs for me. I'm sure their are other reasons, as this program shouldent be going into bad memory. Nothing in this code seems overly suspicious to me, I cant seem to find any rhime or reason to theyse crashs :/.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Since we've no idea how you declared, initialised and assigned values to any of your data, then anything could be going on.

    You say both crash, but that could just mean that you're feeding them both garbage.

    Post a short and complete program which crashes - remove all unused functions before posting. Being able to actually compile and run something you say crashes greatly increases your chances of getting an answer.

  6. #6
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    This blob of a code isint going to be readable by anyone. But you asked for it. Strangely enough, this does exactly what its suposed to. Probably because it does nothing. I'd have to post the whole code, which I'll add to the end of this post.

    Code:
    #include <windows>
    #include <iostream>
    #include <string>
    #include <conio>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    #define Enemy1 '@'
    #define Enemy2 '&' 
    #define Enemy3 '$'
    #define Enemy4 '%'
    
    #define E_Left 10 //Movement Definitions.
    #define E_Right 20
    #define E_Up 30
    #define E_Down 40
    
    #define Blank ' '
    #define Wall1 '|'
    #define Wall2 '/'
    #define Wall3 '\\'
    #define Wall4 '>'
    #define Wall5 '<'
    #define Wall6 '['
    #define Wall7 ']'
    #define Wall8 '-'
    #define Wall9 '='
    #define Wall10 '^'
    
    #define Up 1
    #define Down 2
    #define Left 3
    #define Right 4
    #define Esc 5
    
    HANDLE hOt = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
    
    class Enemy {
     private:
    
     char Ent;
     int ex_pos;
     int ey_pos;
     int tag;
     int mproc;
     SHORT alive;
    
     public:
    
     Enemy() {Ent=0;ex_pos=0;ey_pos=0;tag=0;mproc=0;alive=0;}
    
     Enemy(char t, int ex, int ey, int etag, int moving, SHORT live) {Ent=t; ex_pos=ex;ey_pos=ey;tag=etag;mproc=moving;alive=live;}
    
     COORD GetPos() {COORD m; m.X=ex_pos; m.Y=ey_pos; return m;}
     char GetType() {return Ent;}
     int GetX() {return ex_pos;}
     int GetY() {return ey_pos;}
     int GetTag() {return tag;}
     int GetMovement() {return mproc;}
     SHORT IsAlive() {return alive;}
     SHORT operator==(Enemy X);
     SHORT IsNull();
    
     void SetType(char c) {Ent=c;}
     void SetX(int x) {ex_pos=x;}
     void SetY(int y) {ey_pos=y;}
     void SetTag(int Tag) {tag=Tag;}
     void SetMovement(int Move) {mproc=Move;}
     void SetLive(int live) {alive=live;}
     void Delete();
    };
    
    SHORT Enemy::operator==(Enemy X) {
     if(this->GetType() == X.GetType() && this->GetX() == X.GetX() && this->GetY() == X.GetY() && this->GetTag() == X.GetTag())
      return 1;
    
     return 0;
    }
    
    SHORT Enemy::IsNull() {
     Enemy Null(0, 0, 0, -1, 0, 0);
    
     if(*this == Null)
      return 1;
    
     return 0;
    }
    
    void Enemy::Delete() {
     COORD m;
    
     m.X = this->GetX();
     m.Y = this->GetY();
     SetConsoleCursorPosition(hOt, m);
     cout << ' ';
    }
    
    vector <Enemy> Ens;
    WORD Scolor;
    CONSOLE_SCREEN_BUFFER_INFO inf;
    
    void SetPos(SHORT x, SHORT y, int ix=0, int iy=0) {
     COORD Pos;
     SHORT v;
    
     Pos.X = x;
     Pos.Y = y;
    
     for(v=0; v < ix; v++)
      Pos.X++;
    
     for(v=0; v > ix; v--)
      Pos.X--;
    
     for(v=0; v < iy; v++)
      Pos.Y++;
    
     for(v=0; v > iy; v--)
      Pos.Y--;
    
     SetConsoleCursorPosition(hOt, Pos);
    
     return;
    }
    
    void SetColor(char c) {
     if(c == 'o')
      SetConsoleTextAttribute(hOt, Scolor);
    
     if(c == 'r')
      SetConsoleTextAttribute(hOt, FOREGROUND_RED | FOREGROUND_INTENSITY);
    
     if(c == 'g')
      SetConsoleTextAttribute(hOt, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    
     if(c == 'b')
      SetConsoleTextAttribute(hOt, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
    
     if(c == '1')
      SetConsoleTextAttribute(hOt, BACKGROUND_RED | BACKGROUND_INTENSITY);
    
     if(c == '2')
      SetConsoleTextAttribute(hOt, BACKGROUND_GREEN | BACKGROUND_INTENSITY);
    
     if(c == '3')
      SetConsoleTextAttribute(hOt, BACKGROUND_BLUE | BACKGROUND_INTENSITY);
    
     if(c == 's')
      SetConsoleTextAttribute(hOt, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    }
    
    char ReadPos(SHORT x, SHORT y, SHORT ix=0, SHORT iy=0) {
     DWORD reader;
     COORD Pos;
     char EEE[1];
     SHORT v;
    
     Pos.X = x + ix;
     Pos.Y = y + iy;
    
     ReadConsoleOutputCharacter(hOt, &EEE[0], 1, Pos, &reader);
    
     return EEE[0];
    }
    
    void MoveEnemies() {
     vector<Enemy>::iterator it;
     unsigned int x;
     COORD xy;
     SHORT d;
     Enemy En;
    
     if(Ens.empty())
      return;
    
     for(it = Ens.begin(); it != Ens.end(); it++) {
      En = *it;
      En.SetMovement(E_Left);
      xy=En.GetPos();
      d=En.GetMovement();
    
      if(d == E_Right) {
       SetPos(xy.X, xy.Y, 0, 0);
       cout << Blank;
       SetPos(xy.X, xy.Y, 1, 0);
       En.SetX(xy.X+1);
       SetColor('r');
       cout << En.GetType();
       SetColor('o');
      } else
       if(d == E_Left) {
        SetPos(xy.X, xy.Y, 0, 0);
        cout << Blank;
        SetPos(xy.X, xy.Y, -1, 0);
        En.SetX(xy.X-1);
        SetColor('r');
        cout << En.GetType();
        SetColor('o');
       }
     }
    }
    
    void EvaluateEnemies() {
     unsigned int x, y;
     COORD xy;
     char Yam[2];
     vector<Enemy>::iterator it;
     Enemy en;
    
     if(Ens.empty())
      return;
    
     for(it = Ens.begin(); it != Ens.end(); it++) {
      en = *it;
      xy=en.GetPos();
      Yam[0]=ReadPos(xy.X, xy.Y, 1, 1);
      Yam[1]=ReadPos(xy.X, xy.Y, -1, 1);
      if(Yam[0] == Blank && Yam[1] == Blank)
       en.SetMovement(0);
      if(Yam[0] == Blank)
       en.SetMovement(E_Left);
      if(Yam[1] == Blank)
       en.SetMovement(E_Right);
     }
    }
    
    int EnemyMain() {
     cout << '1';
     EvaluateEnemies();
     cout << '2';
     MoveEnemies();
     cout << '3';
    
     return 0;
    }
    
    int main() {
     EnemyMain();
    }
    As I said, this example doesnt crash. But it wont do anything either. Heres my whole code, do whatever you want with it.

    The upload limit prevents me from uploading all the neccesary files, so save this as EnemyClass.h;
    Code:
    #include "KeeperClass.h"
    
    #define Enemy1 '@'
    #define Enemy2 '&' 
    #define Enemy3 '$'
    #define Enemy4 '%'
    
    #define E_Left 10 //Movement Definitions.
    #define E_Right 20
    #define E_Up 30
    #define E_Down 40
    
    class Enemy {
     private:
    
     char Ent;
     int ex_pos;
     int ey_pos;
     int tag;
     int mproc;
     SHORT alive;
    
     public:
    
     Enemy() {Ent=0;ex_pos=0;ey_pos=0;tag=0;mproc=0;alive=0;}
    
     Enemy(char t, int ex, int ey, int etag, int moving, SHORT live) {Ent=t; ex_pos=ex;ey_pos=ey;tag=etag;mproc=moving;alive=live;}
    
     COORD GetPos() {COORD m; m.X=ex_pos; m.Y=ey_pos; return m;}
     char GetType() {return Ent;}
     int GetX() {return ex_pos;}
     int GetY() {return ey_pos;}
     int GetTag() {return tag;}
     int GetMovement() {return mproc;}
     SHORT IsAlive() {return alive;}
     SHORT operator==(Enemy X);
     SHORT IsNull();
    
     void SetType(char c) {Ent=c;}
     void SetX(int x) {ex_pos=x;}
     void SetY(int y) {ey_pos=y;}
     void SetTag(int Tag) {tag=Tag;}
     void SetMovement(int Move) {mproc=Move;}
     void SetLive(int live) {alive=live;}
     void Delete();
    };
    
    SHORT Enemy::operator==(Enemy X) {
     if(this->GetType() == X.GetType() && this->GetX() == X.GetX() && this->GetY() == X.GetY() && this->GetTag() == X.GetTag())
      return 1;
    
     return 0;
    }
    
    SHORT Enemy::IsNull() {
     Enemy Null(0, 0, 0, -1, 0, 0);
    
     if(*this == Null)
      return 1;
    
     return 0;
    }
    
    void Enemy::Delete() {
     COORD m;
    
     m.X = this->GetX();
     m.Y = this->GetY();
     SetConsoleCursorPosition(hOt, m);
     cout << ' ';
    }
    1.txt needs to be in the c:\ folder.

    Thats everything I think. I'll thank anyone who even attempts to read my coding :P.
    Attached Files Attached Files
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Post a short and complete program which crashes
    ....

  8. #8
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Shortening it doesnt help. The tremor of the crash is directly caused by the code in my first post. Their is no short way to use this code. I cant actualy make a smaller version that reproduces the crash because I dont know what is causing the crash...

    Heres the general idea...

    Declare a global vector. For shortening purposes, an integer.
    Code:
    vector <int> Ens;
    Count the number of enemies on the map. This number happens to be three.
    Code:
    int main() {
     int x;
    
     //Calculate number of occurences, "NOC"
    
     Ens.reserve(NOC);
    
     for(x=0; x <= NOC; x++)
      Ens[x] = GetEnemy();
    
     DoEnemyStuff();
    }
    The next step is to gather the enemy information, set their movement pattern, then move them accordingly. This is where the error lies.

    This wont crash, but heres the idea:

    Code:
    *real*
    void EvaluateEnemies() {
     unsigned int x, y;
     COORD xy;
     char Yam[2];
     vector<Enemy>::iterator it;
     Enemy en;
    
     if(Ens.empty())
      return;
    
     for(it = Ens.begin(); it != Ens.end(); it++) {
      en = *it;
      xy=en.GetPos();
      Yam[0]=ReadPos(xy.X, xy.Y, 1, 1);
      Yam[1]=ReadPos(xy.X, xy.Y, -1, 1);
      if(Yam[0] == Blank && Yam[1] == Blank)
       en.SetMovement(0);
      if(Yam[0] == Blank)
       en.SetMovement(E_Left);
      if(Yam[1] == Blank)
       en.SetMovement(E_Right);
     }
    }
    */real*
    
    *fake*
    void EvaluateEnemies() {
     DWORD reader;
     COORD Pos;
     char EEE[1];
     SHORT v;
     vector <int>::iterator it;
     int en;
    
     if(Ens.empty())
      return;
    
     for(it=Ens.begin(); it != Ens.end(); it++) {
      en = *it;
      ReadConsoleOutputCharacter(hOt, &EEE[0], 1, Pos, &reader);
      if(EEE[0] == Preset_Default)
       en.SetValue(0);
     }
    }
    Thats not a very good example as an integer limits what can happen. This probably wont compile, and it wont crash either. If I COULD reproduce a short example that recreated the crash, I would. But I dont know why this happens to cause a crash.

    My knowledge of this problem doesnt allow me to make any better an example, but the whole example is...
    Code:
    vector <int> Ens;
    
    void EvaluateEnemies() {
     DWORD reader;
     COORD Pos;
     char EEE[1];
     SHORT v;
     vector <int>::iterator it;
     int en;
    
     if(Ens.empty())
      return;
    
     for(it=Ens.begin(); it != Ens.end(); it++) {
      en = *it;
      ReadConsoleOutputCharacter(hOt, &EEE[0], 1, Pos, &reader);
      if(EEE[0] == Preset_Default)
       en.SetValue(0);
     }
    }
    
    int main() {
     int x;
    
     //Calculate number of occurences, "NOC"
    
     Ens.reserve(NOC);
    
     for(x=0; x <= NOC; x++)
      Ens[x] = GetEnemy();
    
     EvaluateEnemies();
    }
    Thats the best short example I can make. The crash glitch is a problem that strings into the code, I cant explain why it crashs, or I wouldent be asking.

    I apologise for the length, but I just cant make a better example.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > for(x=0; x <= NOC; x++)
    Well I can see some running off the end of the array/vector going on here.

  10. #10
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    How can you run off an array you are expanding?

    Oh crap. I miswrote the example -,-.

    The actual code uses pushback;

    Code:
    int main() {
     int x;
    
     //Calculate number of occurences, "NOC"
    
     Ens.reserve(NOC);
    
     for(x=0; x < NOC; x++)
      Ens.push_back(GetEnemy());
    
     EvaluateEnemies();
    }
    Sorry about that, its not possible to run off the end of an array your expanding, unless you hit a cap. If you add in a cout << Ens.size(); you can see the size of the vector. In the actual code, the amount of the array after expansion was three.

    Sorry about messing up that example code >.>. But yeah, I'm positive that the code EvaluateEnemies() and MoveEnemies() are both bugged. The example is a poor reflection of code, but its hard to make a good compact example without actualy knowing the problem :/.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  11. #11
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ok, I narrowed it down. I did a few tests, and I really dont like this at all.

    Code:
     for(it = Ens.begin(); it != Ens.end(); it++) {
      en = *it;
      xy=en.GetPos();
      Yam[0]=ReadPos(xy.X, xy.Y, 1, 1);
      Yam[1]=ReadPos(xy.X, xy.Y, -1, 1);
      if(Yam[0] == Blank && Yam[1] == Blank)
       en.SetMovement(0);
      if(Yam[0] == Blank)
       en.SetMovement(E_Left);
      if(Yam[1] == Blank)
       en.SetMovement(E_Right);
     }
    And

    Code:
     for(it = Ens.begin(); it != Ens.end(); it++) {
      En = *it;
      En.SetMovement(E_Left);
      xy=En.GetPos();
      d=En.GetMovement();
    
       if(d == E_Right) {
       SetPos(xy.X, xy.Y, 0, 0);
       cout << Blank;
       SetPos(xy.X, xy.Y, 1, 0);
       En.SetX(xy.X+1);
       SetColor('r');
       cout << En.GetType();
       SetColor('o');
      } else
       if(d == E_Left) {
        SetPos(xy.X, xy.Y, 0, 0);
        cout << Blank;
        SetPos(xy.X, xy.Y, -1, 0);
        En.SetX(xy.X-1);
        SetColor('r');
        cout << En.GetType();
        SetColor('o');
       }
    They are the tremor of the crash. It gets worse - this code crashs;
    Code:
     for(it = Ens.begin(); it != Ens.end(); it++) {
      En = *it;
     }
    When the for loop is removed, the code goes without a hitch. I'm very confused, is a for loop actualy causing my program to crash?
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > Oh crap. I miswrote the example -,-.
    Here's a tip.
    Post the ACTUAL CODE YOU'RE COMPILING AND TESTING, not some half-assed rubbish from memory made fuzzy by lack of sleep.

  13. #13
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Scroll up six posts.

    The upload limit prevents me from uploading all the neccesary files, so save this as EnemyClass.h;

    Code:
    #include "KeeperClass.h"
    
    #define Enemy1 '@'
    #define Enemy2 '&' 
    #define Enemy3 '$'
    #define Enemy4 '%'
    
    #define E_Left 10 //Movement Definitions.
    #define E_Right 20
    #define E_Up 30
    #define E_Down 40
    
    class Enemy {
     private:
    
     char Ent;
     int ex_pos;
     int ey_pos;
     int tag;
     int mproc;
     SHORT alive;
    
     public:
    
     Enemy() {Ent=0;ex_pos=0;ey_pos=0;tag=0;mproc=0;alive=0;}
    
     Enemy(char t, int ex, int ey, int etag, int moving, SHORT live) {Ent=t; ex_pos=ex;ey_pos=ey;tag=etag;mproc=moving;alive=li  ve;}
    
     COORD GetPos() {COORD m; m.X=ex_pos; m.Y=ey_pos; return m;}
     char GetType() {return Ent;}
     int GetX() {return ex_pos;}
     int GetY() {return ey_pos;}
     int GetTag() {return tag;}
     int GetMovement() {return mproc;}
     SHORT IsAlive() {return alive;}
     SHORT operator==(Enemy X);
     SHORT IsNull();
    
     void SetType(char c) {Ent=c;}
     void SetX(int x) {ex_pos=x;}
     void SetY(int y) {ey_pos=y;}
     void SetTag(int Tag) {tag=Tag;}
     void SetMovement(int Move) {mproc=Move;}
     void SetLive(int live) {alive=live;}
     void Delete();
    };
    
    SHORT Enemy::operator==(Enemy X) {
     if(this->GetType() == X.GetType() && this->GetX() == X.GetX() && this->GetY() == X.GetY() && this->GetTag() == X.GetTag())
      return 1;
    
     return 0;
    }
    
    SHORT Enemy::IsNull() {
     Enemy Null(0, 0, 0, -1, 0, 0);
    
     if(*this == Null)
      return 1;
    
     return 0;
    }
    
    void Enemy::Delete() {
     COORD m;
    
     m.X = this->GetX();
     m.Y = this->GetY();
     SetConsoleCursorPosition(hOt, m);
     cout << ' ';
    }
    1.txt needs to be in the c:\ folder.

    Thats everything I think. I'll thank anyone who even attempts to read my coding :P.
    Attached Files MazeOfPain.cpp (121 Bytes, 2 views)
    1.txt (2.0 KB, 2 views)
    PlatformClass.h (3.3 KB, 1 views)
    MOPEngine.h (17.0 KB, 1 views)
    KeeperClass.h (573 Bytes, 1 views)
    Its up their.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Are you still trying to point to an element of an Enemy vector in a vector<int>::iterator

    You seem to not be telling us the truth or something. Find the exact line that crashes it.

  15. #15
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    >.<

    Their are no integer vectors in my program, the integer one was an example. This is the EXACT line that crashs;

    Code:
     for(it = Ens.begin(); it != Ens.end(); it++) {
      En = *it;
     }
    It doesnt just crash with an iterator either, staticaly it crashs aswell. It could be that the vector is being corrupted somewhere, but thats definantly the problem. It crashs in both functions.

    I have posted my code for download, its all their. Ens is an enemy vector;

    Code:
    vector <enemy> Ens;
    This is the code that contains the for;
    Code:
    vector <enemy> Ens;
    
    void MoveEnemies() {
     vector<Enemy>::iterator it;
     unsigned int x;
     COORD xy;
     SHORT d;
     Enemy En;
    
     if(Ens.empty())
      return;
    
     for(it = Ens.begin(); it != Ens.end(); it++) {
      En = *it;
      En.SetMovement(E_Left);
      xy=En.GetPos();
      d=En.GetMovement();
    
      if(d == E_Right) {
       SetPos(xy.X, xy.Y, 0, 0);
       cout << Blank;
       SetPos(xy.X, xy.Y, 1, 0);
       En.SetX(xy.X+1);
       SetColor('r');
       cout << En.GetType();
       SetColor('o');
      } else
       if(d == E_Left) {
        SetPos(xy.X, xy.Y, 0, 0);
        cout << Blank;
        SetPos(xy.X, xy.Y, -1, 0);
        En.SetX(xy.X-1);
        SetColor('r');
        cout << En.GetType();
        SetColor('o');
       }
     }
    }
    
    void EvaluateEnemies() {
     unsigned int x, y;
     COORD xy;
     char Yam[2];
     vector<Enemy>::iterator it;
     Enemy en;
    
     if(Ens.empty())
      return;
    
     for(it = Ens.begin(); it != Ens.end(); it++) {
      en = *it;
      xy=en.GetPos();
      Yam[0]=ReadPos(xy.X, xy.Y, 1, 1);
      Yam[1]=ReadPos(xy.X, xy.Y, -1, 1);
      if(Yam[0] == Blank && Yam[1] == Blank)
       en.SetMovement(0);
      if(Yam[0] == Blank)
       en.SetMovement(E_Left);
      if(Yam[1] == Blank)
       en.SetMovement(E_Right);
     }
    }
    But I have zero'd it down to the for loop. I just dont get it... Why does that bit of code crash?
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 02:47 PM
  2. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  3. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  4. Errorless Crash bug?
    By Blackroot in forum C++ Programming
    Replies: 3
    Last Post: 01-24-2006, 06:15 PM
  5. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21