2013-07-17 52 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) 

使用指針幫助我 - 謝謝你們。