我有一个自己实现的列表:结构的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的函数 –