0

我正在尝试读取文件并将信息存储在无符号字符数组中。但是,我的程序似乎正在覆盖变量。

A 类标头:

...
public:
    ClassA(void);
    void LoadMemoryBlock(char* block, int bank);
....
private:
    unsigned char upperMemoryBank1[16384];
    unsigned char upperMemoryBank2[16384];
....

A 类文件:

ClassA::ClassA(void)
{
}
...
void ClassA::LoadMemoryBlock(char* block, int bank)
{
    if (bank == 1)
    {
        memcpy(upperMemoryBank1, block, 16384);
    }
    else if (bank == 2)
    {
        memcpy(upperMemoryBank2, block, 16384);
    }
}

B 类标头:

...
private:
    ClassA* classAobject;
...

B 类文件:

ClassB::ClassB()
{
    classAobject = &ClassA();
    ...
}
...
ClassB::StoreFile(ifstream &file)
{
    int position;

    char fileData[16384];

    position = file.tellg();
    file.seekg(HEADER_SIZE, ios::beg);
    position = file.tellg();
    file.read(fileData, 16384);
    position = file.tellg();
    classAobject->LoadMemoryBlock(fileData, 1);
    classAobject->LoadMemoryBlock(fileData, 2);

    position = file.tellg(); // Crashes here
    file.seekg(16384 + HEADER_SIZE, ios::beg);
    ...
}

在我的调试器中观察位置变量表明,在 LoadMemoryBlock 调用之后,它不再像之前那样显示 16400,而是每次都不同的随机数。此外,文件 ifstream 也被 LoadMemoryBlock 调用损坏。所以我猜测 memcpy 正在覆盖它们。

我尝试以不同的方式初始化我的数组,但现在 memcpy 崩溃了!

A 类标头:

...
public:
    ClassA(void);
    void LoadMemoryBlock(char* block, int bank);
....
private:
    unsigned char* upperMemoryBank1;
    unsigned char* upperMemoryBank2;
....

A 类文件:

ClassA::ClassA(void)
{
    upperMemoryBank1 = new unsigned char[16384];
    upperMemoryBank2 = new unsigned char[16384];
}
...
void ClassA::LoadMemoryBlock(char* block, int bank)
{
    if (bank == 1)
    {
        memcpy(upperMemoryBank1, block, 16384); // Crashes here
    }
    else if (bank == 2)
    {
        memcpy(upperMemoryBank2, block, 16384);
    }
}

B 类标头:

...
private:
    ClassA* classAobject;
...

B 类文件:

ClassB::ClassB()
{
    classAobject = &ClassA();
    ...
}
...
ClassB::StoreFile(ifstream &file)
{
    int position;

    char* fileData = new char[16384];

    position = file.tellg();
    file.seekg(HEADER_SIZE, ios::beg);
    position = file.tellg();
    file.read(fileData, 16384);
    position = file.tellg();
    classAobject->LoadMemoryBlock(fileData, 1);
    classAobject->LoadMemoryBlock(fileData, 2);

    position = file.tellg();
    file.seekg(16384 + HEADER_SIZE, ios::beg);
    ...
}

我认为这些方法中至少一种(如果不是两种)应该有效。我究竟做错了什么?

编辑:我已经包含了上面的 ClassA 初始化。

这就是我调用 StoreFile 方法的方式:

bool ClassB::Load(char* filename)
{
    ifstream file(filename, ios::in|ios::binary);

    if(file.is_open())
    {
        if(!StoreFile(file))
        {
            return false;
        }

        file.close();
        return true;
    }

    printf("Could not open file: %s\n", filename);
    return false;
}
4

2 回答 2

2

99% chance the bug is in whatever code initializes the value of the classAobject pointer. If it points to a legitimate instance of a ClassA object, the code should be fine.

Update: Yep. That was it.

classAobject = &ClassA();

This creates a new ClassA object and then stores a pointer to it. But at the end of the statement, it goes out of scope and is destroyed, leaving classAobject holding a pointer to a non-existing object. You want:

classAobject = new ClassA();

Don't forget the rule of three -- delete it in the destructor, allocate a new one on operator= and in the copy constructor. Or better yet, use a more C++ method like a smart pointer, depending on the semantics desired.

于 2012-04-01T21:47:02.547 回答
1

ClassB构造函数中,您正在初始化classAobject指向临时变量地址的指针,该临时变量在构造函数返回后立即变为无效。这就是问题的原因。用于new创建适当的堆对象。

于 2012-04-01T22:05:51.420 回答