2013-07-17 53 views
0

我有一个自己实现的列表:结构的char *成员被strcmp覆盖?

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 o F中的第一次迭代

内的第二个元素这个数据是一些随机的垃圾从我的主内存和我没有任何想法,为什么它的行为这样。另外,这段代码恰好在一个小时前工作。至少我认为这是因为我不记得更改任何代码。

有没有人有想法?

谢谢!

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

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

如果你在C++中工作,那么强烈推荐'std :: string'应用于所有这些原始的'char *','strcmp'内容。 –

+0

我在C++工作,但我不被允许使用std :: string :( – caiuspb

+0

我想看看调用getNewNode的函数 –

回答

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

实际上,这不是我尝试实现getNewNode方法的唯一方法。最初,我在堆栈上创建了一个newNode,如'NodeComposition newNode; newNode-> address ...'。但我认为我有一些缓冲区溢出问题。 但是,调试器在'strcmp'之前显示正确的值。但我想你是对的 - 它必须是内存泄漏。我明天再试一次。谢谢 – caiuspb

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) 

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