【发布时间】:2014-11-14 00:02:21
【问题描述】:
我的程序正确终止,并且根据 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
}
}
}
【问题讨论】:
-
那么,
Grid::init的代码是什么,第 90 行是哪一行? -
……第 90 行是哪一行?
-
实际上,任何时候(this->theGrid)第一次被调用,我都会得到相同的未初始化值,大小为 8 错误。
-
if (!(&(this->theGrid)))是怎么回事?您似乎正在检查成员变量的地址 - 这可能永远不会是非 NULL (即使this是 NULL - 在这种情况下,由于在 NULL 上调用方法,您已经处于危险境地 - @987654328 @ 将会有一些偏移)。 -
@Jay 这应该会揭示您的问题 - 请参阅我的更新答案。