我遇到访问冲突。
我尝试将 vertexBuffer 指向已初始化的int数组,但得到的结果相同。
我已经进行了一些调试,以查看发生了什么,但似乎我的变量未正确初始化。
#include <iostream>
class CmdGame
{
private:
int bufferSizeX, bufferSizeY;
char *buffer;
bool good = false;
int vertexLeng = 0;
int *vertexBuffer;
public:
static CmdGame intialize(int width, int height);
static void clearBuffer(CmdGame window);
static void vetex2I(int x, int y, CmdGame window);
static void drawArrays(int type, CmdGame window);
static void shangeBuffer(CmdGame window);
};
CmdGame CmdGame::intialize(int width, int height)
{
CmdGame Buffer;
Buffer.bufferSizeX = width;
Buffer.bufferSizeY = height;
CmdGame::clearBuffer(Buffer);
Buffer.good = true;
return Buffer;
}
void CmdGame::shangeBuffer(CmdGame window)
{
printf(window.buffer);
}
void CmdGame::vetex2I(int x, int y, CmdGame window)
{
window.vertexBuffer[window.vertexLeng] = y;
window.vertexBuffer[window.vertexLeng + 1] = x;
window.vertexLeng++;
}
void CmdGame::clearBuffer(CmdGame window)
{
system("CLS");
delete window.buffer;
window.buffer = new char[window.bufferSizeX * window.bufferSizeY];
delete window.vertexBuffer;
window.vertexBuffer = new int[99 * 2];
}
void CmdGame::drawArrays(int type, CmdGame window)
{
if (type == 1)
{
for (int i = 0; i < 99; i += 3)
{
}
}
if(type == 0)
{
for (int i = 0; i < 99; i += 2)
{
window.buffer[window.vertexBuffer[i] * window.vertexBuffer[i + 1]] = '#';
}
}
}
int main()
{
CmdGame window = CmdGame::intialize(64, 32);
while (true)
{
CmdGame::clearBuffer(window);
CmdGame::vetex2I(32, 16, window);
CmdGame::drawArrays(0,window);
CmdGame::shangeBuffer(window);
}
}
期望设置vertexBuffer[i]
的值,但这给出了这一点:
抛出异常:写访问冲突。 window.vertexBuffer为0x1110112。
答案 0 :(得分:0)
您忘记了在initialize()
函数中初始化两个缓冲区!没关系,只要您在'clearBuffer'中使用.good
标志即可:
void CmdGame::clearBuffer(CmdGame window)
{
if (window.good) { // Has buffers already …
delete[] window.buffer; // You need delete[] 'cos you used new []!
delete[] window.vertexBuffer;
}
system("CLS");
window.buffer = new char[window.bufferSizeX * window.bufferSizeY];
window.vertexBuffer = new int[99 * 2];
}
然后重试!