作者ric2k1 (Ric)
看板EE_DSnP
标题Re: [问题] homework8
时间Mon Dec 24 18:19:11 2007
※ 引述《bumpwy (bumpwy)》之铭言:
: ※ 引述《ric2k1 (Ric)》之铭言:
: ※ 引述《bumpwy (bumpwy)》之铭言:
: : 想在问一个问题
: : 作业中要我们在 readgraph的memberfunction中allocate the memory for all the Vertex object,
: : but wouldn't these objects be invalid out of the function's scope?
: Local pointer variables may be invalidated. However, the "pointed memory"
: will reside unless you "explicitly" delete it.
: You can do something like:
: _toVertices = new Vertex*[_numToVertices];
: Vertex* tmpPtr[_numToVertices];
: for (size_t i = 0; i < _numToVertices; i++)
: tmpPtr = new Vertex;
: _root = tmpPtr[0];
: ...
: ..
: .
哎呀, 上面那段有点脑残写错了... 大家怎麽没有提出来呢?
我的意思是:
// 先建立 Vertices
Vertex* tmpPtr[_numVertices];
for (size_t i = 0; i < _numVertices; i++)
tmpPtr[i] = new Vertex;
_root = tmpPtr[0];
// 在建立连线
for (size_t i = 0; i < _numVertices; i++) {
// 读 adjMat[i] 计算出 tmpPtr[i]->_numToVertices
// tmpPtr[i]->toVertices = new Vertex* [....];
// 建立连线!!
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.21.241
※ 编辑: ric2k1 来自: 140.112.21.241 (12/24 18:20)
※ 编辑: ric2k1 来自: 59.121.128.3 (12/28 23:25)