结构的char *成员被strcmp覆盖?

时间:2013-07-17 15:38:43

标签: c++ list pointers struct char

我有一个自己实现的列表:

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;
 }

2 个答案:

答案 0 :(得分: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;
 }

答案 1 :(得分: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)

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