Parasoft error: objects to manage resources should be used instead “x” pointer

限于喜欢 提交于 2019-12-11 16:12:52

问题


I've created a binary search tree, each node of my binary tree is setup in a struct containing the key, and a pointer to the left and right nodes.

In my copy constructor for this binary search tree, I call a helper method to recurs through the tree that looks like so:

Node* BinaryTree::copyHelper(const Node* other)
{
if(other == NULL)
{
    return NULL; // If there's no Node to copy, return NULL.
}

Node* newNode  = new Node; 

if(newNode)
{
    newNode->name  = other->name;
    newNode->left  = copyHelper(other->left); 
    newNode->right = copyHelper(other->right);
}

return newNode; 
}

My error mentioned in the title is on the left and right pointers in the final if statement above.

If someone could tell me how to remove it, that would be appreciated.


回答1:


You can probably bypass the warning if you use smart pointers instead of raw pointers:

typedef std::unique_ptr<Node> NodePtr; 
NodePtr newNode(new Node);

instead of

Node* newNode = newNode;


来源:https://stackoverflow.com/questions/10414869/parasoft-error-objects-to-manage-resources-should-be-used-instead-x-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!