Search code examples
c++strcmp

Saving & Loading my modules


So I'm trying to rewrite my old Memory class I made a while back, but stumbling upon some problems within the strcmp statement. This is my SaveModules function:

void Memory::AddModuleToList(char* modSaveName, LPSTR ModName, int num) 
{
    this->Modules[num] = this->Module(ModName);
    this->modNames[num] = modSaveName;
}

And this is the way I load them:

    DWORD Memory::LoadSavedModule(char* modName) {
    for (int i = 0; i < 128; i++) {
        if (strcmp(modName, modNames[i])) {
            return Modules[i];
        };
    }
}

Reason I loop through 128 is because my Modules array has 128 slots. Now the problem with my code is that strcmp is causing a breakpoint with the follolwing error;

Access violation reading location 0xCDCDCDCD.

(Full Error)

Unhandled exception at 0x0FAF1F52 (msvcr110d.dll) in Memory Test.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD.

EDIT (2015-05-13): I think I may have just figured it out, my Modules[128] is undefined everywhere except Num, so when using strcmp it cant compare and throws a exception, am I correct?

How I solved it:

    void Memory::AddModuleToList(char* modSaveName, LPSTR ModName, int num, bool firstTime) 
{
    if (firstTime) {
        for (int i = 0; i < 128; i++) {
            this->modNames[i] = "";
            this->Modules[i] = 0;
        };
    };
    this->Modules[num] = this->Module(ModName);
    this->modNames[num] = modSaveName;
}

Solution

  • You're not initialising the Modules array - that value 0xCDCDCDCD is a flag put there by the C runtime system to signal the memory is uninitialised.