0

In my user made link list which I'm using to hold an inventory for the text based rpg I am making, I am encounter an error with my delete function, within my link list. I was wondering if someone could point out to me why I am getting this error.

void InventoryList::deleteNode(int num)
{
    ListNode *previousNode; //To point to the previous node
    ListNode *nodePtr; //to traverse the list

    int number = 1;

    //if the head is empty do nothing
    if (!head)
    {
        return;
    }
    //Determine if the first node is the value
    if (1 == num)
    {

        nodePtr = head->next;
        delete head;
        head = nodePtr;
    }
    else
    {
        //intialize the node as head.
        nodePtr = head;


        //Skip nodes whose value is not equal to num.
        while (nodePtr != nullptr && number != num)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;

            number++;
        }
        if (nodePtr)
        {
        previousNode->next = nodePtr->next;
        delete nodePtr;
        }
    }
}

I am encountering this error with the final if statement of the code, and the error is:

error C4703: potentially uninitialized local pointer variable 'previousNode' used

3
  • it is calling that error because you only initialize previousNode inside of a while loop and while loops do not always run (ie, if num == 0). set previousNode=nodePtr before the while loop as well as inside of it Commented Nov 6, 2015 at 23:47
  • 2
    If your focus is an RPG game, use the existing std::list and relieve yourself of these burdens from reinventing your own linked list. Commented Nov 6, 2015 at 23:47
  • Thanks alot! And I know it'd be easier to use the std::list, but I need to make one for my class(college class Commented Nov 7, 2015 at 0:09

1 Answer 1

1

R Nar's comment has it right. The while loop where you initialize PreviousNode isn't guaranteed to run, so the pointer is potentially uninitialized - thus, your error message.

To fix it, you need to initialize PreviousNode in a block that is guaranteed to run - no if/while/etc.

This could go anywhere, but I'd recommend setting it to NULL when you declare it, and check to ensure it's not null when you use it. Always a good habit to initialize when you declare and check your pointers.

Sign up to request clarification or add additional context in comments.

1 Comment

The only way the loop is not going to run is if nodePtr == nullptr. Or number == num. We know that it's not number == num (as it wouldn't get to that point if that was the case) so nodePtr == nullPtr is the only case in which the loop won't run. If that happens, then the 'if' guard after the loop will ensure the problematic statement won't be executed. The code is correct, but the compiler's static analysis isn't clever enough to realise that. (Notice also that C4703 is not an error, but a level 4 warning. It is an error because the flag to treat warnings as errors was used.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.