我有一個自己實現的列表:結構的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;
}
如果你在C++中工作,那麼強烈推薦'std :: string'應用於所有這些原始的'char *','strcmp'內容。 –
我在C++工作,但我不被允許使用std :: string :( – caiuspb
我想看看調用getNewNode的函數 –