Errorless Crash bug?

This is a discussion on Errorless Crash bug? within the C++ Programming forums, part of the General Programming Boards category; [BORLAND C++ free command line compiler] [console program] Ok, until a few hours ago, I had a perfectly functional program. ...

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

    Errorless Crash bug?

    [BORLAND C++ free command line compiler]

    [console program]

    Ok, until a few hours ago, I had a perfectly functional program. My header contains most of the functions, I debugged enough to find a rough source;

    Code:
    void ClearAsyncArrowBuffer() {
     if(GetAsyncKeyState(VK_UP)&SHRT_MAX)
      cout << "";
     if(GetAsyncKeyState(VK_DOWN)&SHRT_MAX)
      cout << "";
     if(GetAsyncKeyState(VK_LEFT)&SHRT_MAX)
      cout << "";
     if(GetAsyncKeyState(VK_RIGHT)&SHRT_MAX)
      cout << "";
    }
    
    int ATCONFIRM() {
     int x;
    
     ClearAsyncArrowBuffer();
    
     for(x=0; x <= 1000; x++)
      if(GetAsyncKeyState(VK_LEFT)&SHRT_MAX)
       return 1;
      else
       Sleep(10);
    
     return 0;
    }
    
    
    int Save() {
     string MAP;
     char Saveme[1];
     DWORD reader;
     COORD Pos;
     unsigned int x_pos, y_pos, x00;
    
     x00=0;
    
     for(y_pos=0; y_pos <= 24; y_pos++)
      for(x_pos=0; x_pos <= 79; x_pos++) {
       Pos.X = x_pos;
       Pos.Y = y_pos;
       ReadConsoleOutputCharacter(hOt, &Saveme[0], 1, Pos, &reader);
       if(x_pos == 79)
        MAP[x00] = 'W';
       else
        MAP[x00] = Saveme[0];
       x00++;
      }
    
     if(!ATCONFIRM())
      return 0;
    
     ofstream file_out("C:\\1.txt");
    
     if(!file_out) {
      MessageBox(NULL, "Error[2], could not open file!", "Error[2]", MB_OK);
      return -1;
     }
    
     for(x00=0; x00 <= MAP.length(); x00++)
      if(MAP[x00] == 'W')
       file_out << '\n';
      else
       file_out << MAP[x00];
    
     file_out.close();
     MessageBox(NULL, "File Saved Successfuly.", "Sucess!", MB_OK);
     return 1;
    }
    
    The part in red is what I have found to be the problem, I'll post the whole code at the end of this post though.

    Ok, I have tried a fair bit, this should unless I'm mistaken, save what you're currently seeing in the console. My problem is, when I press the right arrow, then left (which is how the program knows you want to save) it just stops. The whole program exits. But, in that part of code, I have no exit calls, and no breaks either. I cant figure why this happens.

    *I know this is absolutely 100% nonportable, but I'm sort of addicted to windows.h -,-*

    And, the header pritty big... I'll post it if I need to, but I'd rather not, the .cpp file just calls the header functions so its pritty useless to post.

    I'm 99% sure that save function is the problem. (My debugging shows that it successfuly entered into the save function, but it never actualy continued afterwards...?)

    Well, thank you very much, this is a really weird error.
    *I would post my error, but I'm not getting one, it just stops the program.*
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm fairly certain your problem is in the way you use the string (it's a std::string, correct?)

    You cannot treat a string like a map, even if you name it MAP. If all you are doing is adding characters/strings to the end of it, you can use the += operator:
    Code:
    if(x_pos == 79)
        MAP[x00] = 'W';
    //change to
    if(x_pos == 79)
        MAP+= 'W';
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    It works, but I'm confused...

    Code:
    string h;
    
    h+='1'; //Same as h=h+'1' right?
    
    h[0]='1';
    That does the same thing right? You're assinging the first character in h a value of '1'. So why does it not work both ways??

    Well, thank you very much! It should give you an error when it crashs like that -,-.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    When you create your string h, it is empty, meaning it has no characters. When you access the first character with h[0], that causes a problem because that character doesn't exist. You can only use [] to index a string (or array or vector) if the string has something at that spot.

    On the other hand, += adds a new character to the end of the string, so if you use += it will not have a problem because it adds the character itself.

    C++ doesn't give an error in that situation because it is trying to save time. If it has to check the size of the string every time you try to access it it would slow down the program.

    The compiler doesn't give an error because how would it know whether your string has a character in that position or not, especially if the code is more complicated (e.g. getting string input from the user). In other code the h[0] = '1' might be perfectly valid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  2. Hooking Crash?
    By Elysia in forum Windows Programming
    Replies: 9
    Last Post: 03-15-2008, 01:13 PM
  3. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  4. Another crash bug?
    By Blackroot in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2006, 07:18 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