我的程序正确终止,根据Valgrind的说法,没有泄露的内存。
但是第一次运行对象方法时会出现以下消息。
Use of uninitialised value of size 8
获取有关上述事件的更多详细信息
==18787== Use of uninitialised value of size 8
==18787== at 0x4017F3: Grid::init(int) (grid.cc:90)
==18787== by 0x401D2A: main (main.cc:43)
==18787== Uninitialised value was created by a stack allocation
==18787== at 0x401BE4: main (main.cc:11)
我还看到以下消息:
Conditional jump or move depends on uninitialised value(s)
Invalid free() / delete / delete[] / realloc()
Address 0x7ff000b08 is on thread 1's stack
这是Grid :: init
的代码void Grid::init(int n){
if (!(&(this->theGrid))) { this->clearGrid(); } //If non-empty, destroys grid
this->theGrid = new Cell*[n]; //Create new grid of size n x n
this->td = new TextDisplay(n); //new Text Display
for (int i = 0; i < n; i++){
this->theGrid[i] = new Cell[n];
for(int j = 0; j < n; j++){ //Cell initializations
(this->theGrid[i][j]).setDisplay(this->td); //set display
(this->theGrid[i][j]).setCoords(i, j); //set co-ordinates
(this->theGrid[i][j]).setState(0); //default state
}
}
}
答案 0 :(得分:2)
我猜你的代码看起来像是:
条件跳转或移动取决于未初始化的值:
bool value /* nothing */;
if (value) {
}
免费无效:
char* buf = new char[10000];
buf = &x;
delete [] buf;
现在您发布了代码,我假设第90行是这一行:
if (!(&(this->theGrid))) { this->clearGrid(); } //If non-empty, destroys grid
问题是您没有在构造函数中初始化theGrid
到nullptr
,然后您最终可能delete[]
- 非new[]
- ed数组。此外,如果您刚刚执行了以下操作,那么代码会更清晰:
if (!theGrid) {
clearGrid();
}
到处都不需要this->
。我不明白为什么你要使用theGrid
的地址。