Potentially uninitialized local pointer variable 'node' used. C++

二次信任 提交于 2019-12-12 04:59:43

问题


I am really new to C++ and this code was an example in my book so it should work because I have to implement a few new functions into this. However, I copied the code line for line and I keep getting this error message now my code won't compile until I fix it.

It says that my local pointer 'node' is being used. I don't know what this actually means. Could anyone tell me whats is the error actually telling me? Also could some one help me fix this so I can start my project? I'm only asking for the code because this isn't part of my project, it was already given by the teacher.

Here is my code:

// ListNode.h
#ifndef _LISTNODE_H
#define _LISTNODE_H

#include <cstdlib>

typedef int ItemType;

class ListNode {
    friend class LList;

public:
    ListNode(ItemType item, ListNode* link = NULL);

private:
    ItemType item_;
    ListNode *link_;
};

inline ListNode::ListNode(ItemType item, ListNode *link)
{
    item_ = item;
    link_ = link;
}

#endif // _LISTNODE_H

// LList.h
#ifndef _LLIST_H
#define _LLIST_H

#include "ListNode.h"

class LList {

public:
    LList();
    LList(const LList& source);
    ~LList();

    LList& operator=(const LList& source);
    int size() { return size_; }
    void append(ItemType x);
    void insert(size_t i, ItemType x);
    ItemType pop(int i = -1);
    ItemType& operator[](size_t position);

private:
    // methods
    void copy(const LList &source);
    void dealloc();
    ListNode* _find(size_t position);
    ItemType _delete(size_t position);

    // data elements
    ListNode *head_;
    int size_;
};

#endif // _LLIST_H

// LList.cpp
#include "LList.h"

LList::LList()
{
    head_ = NULL;
    size_ = 0;
}

ListNode* LList::_find(size_t position)
{
    ListNode *node = head_;
    size_t i;

    for (i = 0; i<position; i++) {
        node = node->link_;
    }
    return node;
}

ItemType LList::_delete(size_t position)
{
    ListNode *node, *dnode;
    ItemType item;

    if (position == 0) {
        dnode = head_;
        head_ = head_->link_;
        item = dnode->item_;
        delete dnode;
    }
    else {
        node = _find(position - 1);
        if (node != NULL) {
            dnode = node->link_;
            node->link_ = dnode->link_;
            item = dnode->item_;
            delete dnode;
        }
    }
    size_ -= 1;
    return item;
}

void LList::append(ItemType x)
{
    ListNode *node, *newNode = new ListNode(x);

    if (head_ != NULL) {
        node = _find(size_ - 1);
        node->link_ = newNode;
    }
    else {
        head_ = newNode;
    }
    size_ += 1;
}

void LList::insert(size_t i, ItemType x)
{
    ListNode *node;

    if (i == 0) {
        head_ = new ListNode(x, head_);
    }
    else {
        node = _find(i - 1);
        node->link_ = new ListNode(x, node->link_);
    }
    size_ += 1;
}

ItemType LList::pop(int i)
{
    if (i == -1) {
        i = size_ - 1;
    }
    return _delete(i);
}

ItemType& LList::operator[](size_t position)
{
    ListNode *node;

    node = _find(position);
    return node->item_;
}

LList::LList(const LList& source)
{
    copy(source);
}

void LList::copy(const LList &source)
{
    ListNode *snode, *node;

    snode = source.head_;
    if (snode) {
        node = head_ = new ListNode(snode->item_);
        snode = snode->link_;
    }
    while (snode) {
        node->link_ = new ListNode(snode->item_);
        node = node->link_;
        snode = snode->link_;
    }
    size_ = source.size_;
}

LList& LList::operator=(const LList& source)
{
    if (this != &source) {
        dealloc();
        copy(source);
    }
    return *this;
}

LList::~LList()
{
    dealloc();
}

void LList::dealloc()
{
    ListNode *node, *dnode;

    node = head_;
    while (node) {
        dnode = node;
        node = node->link_;
        delete dnode;
    }
}

I know where the problem is exactly (Line 104 in my code)

node->link_ = new ListNode(snode->item_);

This part of my code is the problem. Could anyone help me fix this problem so I can work on my program? Thanks!

Now that my previous problem was answered I have a new one.

How would I go about testing my code? I have a few lines but it keeps coming out with errors when I try to print out the contents of my LList. This is my test code:

#include "LList.h"

int main()
{
    LList b, c;
    int x;

    b.append(1);
    b.append(2);
    b.append(3);
    c.append(4);
    c.append(5);
    c = b;
    x = b.pop();
} 

Could anyone help me write a working test code, this the last thing I will need to start adding my different functions.


回答1:


This is probably due to warnings being treated as errors in your build. The compiler is complaining that node might not be initialized when node->link is accessed. However, that won't actually be the case because if snode is null, the while block won't be accessed, so you won't access the uninitialized memory. If you want to make the warning go away, putting the while loop inside the if block will probably work:

snode = source.head_;
if (snode) {
    node = head_ = new ListNode(snode->item_);
    snode = snode->link_;

    while (snode) {
        node->link_ = new ListNode(snode->item_);
        node = node->link_;
        snode = snode->link_;
    }
}



回答2:


This is a false warning as the compiler seems to think you are going to use the node without having assigned it (because it might skip the if statement). In this scenario I do not see how this would happen as the while (snode) would never be entered.

You could simply ignore this warning (disable treat warnings as errors in the project settings) or remove this warning from this specific part of code (#pragma warning(disable : 4703)).

To build without treating warnings as errors go to: Project -> Properties -> C/C++ -> Treat warnings as errors. Disable this option or use the pragma directives.



来源:https://stackoverflow.com/questions/29749206/potentially-uninitialized-local-pointer-variable-node-used-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!