0

我有一个自己的实施清单:

struct NodeComposition {
    Int32 index;
    Int8 address;
    char* label;
    NodeComposition* next;
};

我正在使用以下方法创建新结构,而根元素的标签用 NULL 初始化,稍后会更改。

NodeComposition ListManager::getNewNode(char* label, Int8 address)
{
    NodeComposition* newNode = new NodeComposition;
    newNode->address = address;
    newNode->label = label;
    newNode->next = 0;
    newNode->index = -1;
    return *newNode;
}

为了检查是否存在特定的“标签”,我实现了以下方法:

NodeComposition* ListManager::labelExists(char* label)
{
UInt32 i = 0;
NodeComposition* conductor = &rootNode;

// Traverse through list
while(i < elements)
{
    // Label has been found

    if (strcmp(conductor->label, label) == 0)
    {
        return conductor;
    }

    /* Advancing in list */
    else
    {
        if(conductor->next != 0)
        {
            conductor = conductor->next;
        }

        else
        {
            /* Error: Null reference found in conductor->next */
            return NULL;
            //return Errors::NULL_REFERENCE;
        }
    }

    i++;
}
/* label not found */
return NULL;
}

我的问题来了:

  • 我调用了该labelExists(char* label)方法(带有两个元素的链表)
  • 在比较两个字符串之后,它会在第一次迭代中更改第二个元素的成员的label

这些数据是我主内存中的一些随机垃圾,我不知道它为什么会这样。此外,该代码恰好在一小时前工作。至少我认为它确实如此,因为我不记得更改任何代码。

有人有想法吗?

谢谢!

编辑:这是一些额外的代码

NodeComposition newNode = getNewNode(label, address);
ListManager::addNode(newNode);


Int32 ListManager::addNode(NodeComposition node)
{
node.index = elements;
lastNode->next = &node;
lastNode = &node;
elements++;
return lastNode->index;
 }
4

2 回答 2

2

绝对不是strmcp,所以我们不要关注它。您应该先清理此代码。存在内存泄漏和损坏。

首先:

NodeComposition ListManager::getNewNode(char* label, Int8 address)
{
    NodeComposition* newNode = new NodeComposition; // $#!^!memory allocated
    newNode->address = address;
    newNode->label = label;  // $#!^! is label allocated on stack or heap? possible leak & corruption
    newNode->next = 0;
    newNode->index = -1;
    return *newNode; // $#!^!return by value. newNode is now lost! memory leak
}

然后在您的附加代码中:

NodeComposition newNode = getNewNode(label, address); // $#!^! getting a copy of the "newNode" only. This copy is allocated in stack.   
ListManager::addNode(newNode); //$#!^! adding a stack object onto linked list


Int32 ListManager::addNode(NodeComposition node)
{
    node.index = elements;
    lastNode->next = &node;
    lastNode = &node;  //node is actually allocated from stack, not heap! likely memory corruption here!
    elements++;
    return lastNode->index;
 }
于 2013-07-17T16:45:20.693 回答
0

我得到了答案。我像这样修改了我的代码:

Int32 ListManager::addNode(NodeComposition* node)
{
node->index = ++elements;
lastNode->next = node;
lastNode = node;
return lastNode->index;
}

NodeComposition* ListManager::getNewNode(char* label, Int8 address)
{
NodeComposition* newNode = new NodeComposition;
newNode->address = address;
newNode->label = label;
newNode->next = 0;
newNode->index = -1;
return newNode;
}

NodeComposition* ListManager::labelExists(char* label)

指针的使用帮助了我 - 谢谢你们。

于 2013-07-18T14:40:36.230 回答