【发布时间】:2013-03-18 18:46:40
【问题描述】:
我正在研究四叉树的复制构造函数。到目前为止,这是我所拥有的:
//Copy Constructor
Quadtree :: Quadtree(const Quadtree & other)
{
root = copy(other.root);
resolution = other.resolution;
}
//Copy Constructor helper function
Quadtree::QuadtreeNode *Quadtree :: copy (const QuadtreeNode* newRoot)
{
if (newRoot != NULL)
{
QuadtreeNode *node = new QuadtreeNode(newRoot->element);
node->nwChild = copy(newRoot->nwChild);
node->neChild = copy(newRoot->neChild);
node->swChild = copy(newRoot->swChild);
node->seChild = copy(newRoot->seChild);
return node;
}
else
return NULL;
}
我不确定我哪里出错了,但是我收到了内存泄漏,Valgrind 指出我有未初始化的值。请帮忙?
附加的是我的 buildTree 函数 - 我实际创建树的地方。我可能在这里做错了什么?
void Quadtree :: buildTree (PNG const & source, int theResolution)
{
buildTreeHelp (root, 0, 0, theResolution, source);
}
void Quadtree :: buildTreeHelp (QuadtreeNode * & newRoot, int xCoord, int yCoord, int d, PNG const & image)
{
if (d == 1)
{
RGBAPixel pixel = *image(xCoord, yCoord);
newRoot = new QuadtreeNode(pixel);
return;
}
newRoot = new QuadtreeNode ();
newRoot = NULL;
buildTreeHelp(newRoot->nwChild, xCoord, yCoord, d/2, image);
buildTreeHelp(newRoot->neChild, xCoord + d/2, yCoord, d/2, image);
buildTreeHelp(newRoot->swChild, d/2, yCoord + d/2, d/2, image);
buildTreeHelp(newRoot->seChild, d/2 + xCoord, d/2 + yCoord, d/2, image);
}
【问题讨论】:
-
你能发布一个完整的小例子吗?您提供的代码不足以证明内存泄漏或未初始化的访问。
-
我添加了实际构建树的 buildTree 函数。也许我初始化树的方式有问题?