这是一个模拟器,但现在我只是在加载 ROM。我不明白为什么我的代码给我一个中断异常错误。gameBuffer 变量在 bufferGame 函数之后超出范围。我可以在该函数中调用 gameBuffer[0] 等,但只要我创建另一个函数来调用数组,它就会给我:SNES Emulator.exe 中 0x0093303A 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000008。请帮忙!
标题:
class CGame
{
public:
CGame(char* passed_gamePath);
~CGame(void);
void loadGame(char* gamePath);
void closeGame();
BYTE* getBufferedFile();
int getGameSize(FILE* game);
void bufferGame(FILE* game);
void printHexBytes(int offset, int amount);
private:
int gameSize;
FILE* game;
BYTE* gameBuffer;
};
CPP 文件: 构造函数:
CGame::CGame(char* passed_gamePath)
{
game = NULL;
gameSize = 0;
gameBuffer = NULL;
loadGame(passed_gamePath);
bufferGame(game);
}
加载游戏:
void CGame::loadGame(char* gamePath)
{
if ((fopen_s(&game, gamePath, "rb")) == 0)
{
std::cout << "File opened successfully!" << std::endl;
gameSize = getGameSize(game);
}
else
{
std::cout << "Could not open file." << std::endl;
}
}
缓冲游戏:
void CGame::bufferGame(FILE* game)
{
gameBuffer = new BYTE[gameSize];
if (fread_s(gameBuffer, gameSize, 1, gameSize, game) != 0)
{
std::cout << "Read file successfully!" << std::endl;
}
else
{
std::cout << "Error reading file." << std::endl;
}
}
*编辑:这是附加代码,因为你们认为上面是正确的。
模拟器在 main 中实例化。构造函数实例化 Game 的一个实例。然后调用 printHexBytes,这是我在读取 gameBuffer 时遇到错误的地方。(顺便说一句,我已将游戏(变量)重命名为 gameFile 以防止混淆。)
模拟器构造函数:
CEmulator::CEmulator(void)
{
CGame* Game = new CGame("SuperMarioWorld.smc");
p_Register = 0x00;
}
主文件:
int main (void)
{
CEmulator* Emulator = new CEmulator();
Emulator->getGame()->printHexBytes(0x0FF, 9);
std::cin.get();
delete Emulator;
return 0;
}
打印十六进制字节(CGame 的一部分):
void CGame::printHexBytes(int offset, int amount)
{
for (int i = offset; i < (offset + amount); i++)
{
printf("%X ", gameBuffer[0]);
}
}