作者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)