caiuspb
caiuspb

Reputation: 974

The char* member of a struct gets overwritten by strcmp?

i've got an own implemented list:

struct NodeComposition {
    Int32 index;
    Int8 address;
    char* label;
    NodeComposition* next;
};

and I am creating new structs with the following method, whereas the label of the root element is initialized with NULL and is changed later on.

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

In order to check if a specific "label" exists I have implemented following method:

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

And here comes my problem:

This data is some random trash out of my main memory and I do not have any idea why it behaves like that. Additionally, exactly this code worked just an hour before. At least I think that it did because I can not remember changing any code.

Does anybody has an idea?

Thank you!

Edit: here is some additional code

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

Upvotes: 0

Views: 511

Answers (2)

caiuspb
caiuspb

Reputation: 974

I got the answer .. I modified my code like this:

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)

The use of pointers helped me out - Thank you guys.

Upvotes: 0

ROTOGG
ROTOGG

Reputation: 1136

It is definitely not strmcp, so let's not focus on that. You should clean up this code first. There are memory leak and corruption going on.

To start with:

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
}

Then in your additional code:

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

Upvotes: 2

Related Questions