Debug Assertion Failed / Vector Subscript out of range



  • Hallo,

    Expression: Vector Subscript out of range Diesen Runtime Fehler bekomme ich.

    Programmiere mir grade Pacman mit der SDL. Es läuft auch soweit bloß nach mehreren Sekunden spielen kommt der Runtime Fehler. Ich bin das mal mit dem Debugger durchgegegangen und an dieser Stelle gibt er mir einen Fehler:

    bool XFontMap::Initialize(char* File, int Pointsize, int Bit) {
    FontControl = TTF_OpenFont(File, Pointsize);
    
    if(FontControl == NULL) {
    cerr << "XFontMap::Initialize > File not found '" << File << "'\n";
    return false;
    }
    
    char tempChar[1];
    
    int tempWidth = 0;
    
    for(int i = 0;i < 255;i++) {
    sprintf(tempChar, "%c", (char)i);
    TTF_SizeText(FontControl, tempChar, &tempWidth, NULL);
    
    if(tempWidth > CharWidth) CharWidth = tempWidth;
    
    }
    
    CharHeight = TTF_FontHeight(FontControl);
    
    Surf_FontMap = SDL_CreateRGBSurface(SDL_HWSURFACE, CharWidth, CharHeight * 255, Bit, RMASK, GMASK, BMASK, AMASK);
    Surf_FontMap = SDL_DisplayFormat(Surf_FontMap);
    
    ClearImage(Surf_FontMap, SDL_MapRGB(Surf_FontMap->format, FontBGColor.r, FontBGColor.g, FontBGColor.b));
    
    return true;
    };
    

    Am Ende dieser Funktion sagt er mir:
    Run-Time Check Failure #2 - Stack around the variable 'tempChar' was corrupted.

    Finde den Fehler im Moment einfach nicht.

    MfG
    Rennmoehre



  • sprintf(tempChar, "%c", (char)i);

    hier schreibst du 2 chars in tempChar rein, da passt aber nur 1 char rein.

    Du kannst dir hier sprintf auch sparen (printf solltest du in C++ sowieso nicht verwenden) und einfach:

    char tempChar[2]={0};
    for(...) {
      tempChar[0]=c;
    }
    

    machen.



  • Ok das hat den Fehler behoben danke 🙂 bekomme trotzdem noch nen Assertion Fail beim spielen kann aber diesmal die Stelle nicht finden .



  • ...


Anmelden zum Antworten