10 Replies - 366 Views - Last Post: 09 February 2018 - 06:08 PM Rate Topic: -----

#1 kiddjsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 06-June 17

error C4703: potentially uninitialized local pointer variable

Posted 09 February 2018 - 12:08 PM

Need some assistance in understanding how to resolve the local pointer error, error C4703: potentially uninitialized local pointer variable at line 93 with this line of code here.

curr->setNext(tmp->getNext());




#include <iostream>
using namespace std;


//looks like this:
// ---------              ---------
// -  x    -              -   x  -
// ---------              ---------
// -Pointer-  -------->   -Pointer-
// ---------              ----------
//
//Individual node class. Contains one integer x value and a pointer to the next node
class Node
{
private:
	int x;
	Node* next;
public:
	Node() {};
	Node(int y) { x = y; next = NULL; }
	static Node* freelist;
	void setVal(int data) { x = data; }
	void setNext(Node* aNext) { next = aNext; };
	int getVal() { return x; };
	Node* getNext() { return next; };
	void * operator new(size_t size){ return new char[size]; }
	void operator delete(void *ptr){ free(ptr); }
};

Node* Node::freelist = NULL;

//Linked List class. Contains head node which is initially set to null
class LinkedList
{
private:
	Node *head;
public:
	LinkedList(){ head = NULL; };
	void Print();
	void Append(int y);
	void Delete(int y);
};

//Append a node to the list
void LinkedList::Append(int y)
{
	//Create a new node and set the integer data value
	Node* newNode = new Node();
	newNode->setVal(y);
	newNode->setNext(NULL);

	Node *tmp = head;//Create a temp pointer

	//Search through the list
	if (tmp != NULL) {
		while (tmp->getNext() != NULL){
			tmp = tmp->getNext();
		}
		// point the last node to the new node
		tmp->setNext(newNode);
	}
	else{
		//First node in the linked list
		head = newNode;
	}


}

//Delete a node from the list
void LinkedList::Delete(int y) {
	Node *tmp = head; //Create a temp pointer
	if (tmp == NULL) //No nodes
		return;

	if (tmp->getNext() == NULL) //Last node
	{
		delete tmp;
		head = NULL;
	}
	else{
		Node *curr;
		//Search through nodes
		while (tmp != NULL)
		{
			if (tmp->getVal() == y)
				break;
			curr = tmp;
			tmp = tmp->getNext();
		}


		curr->setNext(tmp->getNext());
		delete tmp; //delete current node
	}



}

void LinkedList::Print()
{
	Node *tmp = head;
	if (tmp == NULL)
	{
		cout << "Empty" << endl;
		return;
	}
	do{
		cout << tmp->getVal();
		cout << " -->";
		tmp = tmp->getNext();
	} while (tmp != NULL);
	cout << "NULL" << endl;
}

int main()
{
	LinkedList newLinkedList;
	newLinkedList.Append(42);
	newLinkedList.Print();
	newLinkedList.Append(17);
	newLinkedList.Print();
	newLinkedList.Append(21);
	newLinkedList.Print();
	newLinkedList.Append(1701);
	newLinkedList.Print();
	newLinkedList.Delete(1701);
	newLinkedList.Print();
	newLinkedList.Delete(17);
	newLinkedList.Print();
	newLinkedList.Delete(21);
	newLinkedList.Print();
	newLinkedList.Delete(42);
	newLinkedList.Print();
	newLinkedList.Append(42);
	newLinkedList.Print();
	newLinkedList.Append(17);
	newLinkedList.Print();
	newLinkedList.Delete(17);
	newLinkedList.Print();
	newLinkedList.Delete(42);
	system("pause");
}



Is This A Good Question/Topic? 0
  • +

Replies To: error C4703: potentially uninitialized local pointer variable

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2601
  • View blog
  • Posts: 4,155
  • Joined: 21-June 11

Re: error C4703: potentially uninitialized local pointer variable

Posted 09 February 2018 - 12:48 PM

I think the message is telling you that curr would be uninitialized if the while loop never runs. It looks like the while loop will always run at least once (because you checker whether tmp == NULL earlier), so this appears to be a false positive. Either way you should be able to calm the compiler (and inattentive readers) by replacing the while loop with a do-while loop.

Additionally, there are two conditions under which your while loop will end: Either tmp->getVal() == y or tmp == NULL. In the latter case tmp->getNext() would dereference a null pointer and thus cause undefined behavior.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: error C4703: potentially uninitialized local pointer variable

Posted 09 February 2018 - 01:06 PM

I think you need to recheck your logic for that function.

void LinkedList::Delete(int y) {
	...
	else{
		Node *curr;
		//Search through nodes
		while (tmp != NULL)
		{
			if (tmp->getVal() == y)
				break;
			curr = tmp;
			tmp = tmp->getNext();
		}


		curr->setNext(tmp->getNext());
		delete tmp; //delete current node
	}

What will happen if this else statement executes and tmp->getVal() is never equal to y?

I haven't looked at all of your logic so I have a couple of questions.

First where are you setting tmp to nullptr? Note: you should be using nullptr instead of NULL if you're writing a modern C++ program.

Second if tmp does evaluate to nullptr why would you need to delete tmp?

Jim
Was This Post Helpful? 0
  • +
  • -

#4 kiddjsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 06-June 17

Re: error C4703: potentially uninitialized local pointer variable

Posted 09 February 2018 - 03:09 PM

Ok, so I've made some modifications to my code in these two sections below, the code compiles, though at runtime am now getting the following kind of error:

Unhandled exception at 0x001A4F66 in virtualMemory.exe: 0xC0000005: Access violation reading location 0xFEEEFEEE.

the error is thrown at line 24 with this line of code: int getVal() { return x; };


//Individual node class. Contains one integer x value and a pointer to the next node
class Node
{
private:
	int x;
	Node *next;
public:
	Node() {};
	Node(int y) { x = y; next = NULL; }
	static Node *freelist;
	void setVal(int data) { x = data; }
	void setNext(Node* aNext) { next = aNext; };
	int getVal() { return x; };
	Node *getNext() { return next; };
	void *operator new(size_t size){ return new char[size]; }
	void operator delete(void *ptr){ free(ptr); }
	static void printFreelist();
};



//Delete a node from the list
void LinkedList::Delete(int y) 
{
	Node *tmp = head; //Create a temp pointer
	if (tmp == NULL) //No nodes
		return;

	if (tmp->getVal() == y)
    {
      head = head->getNext();
      delete tmp;
    }

	if (tmp->getNext() == NULL) //Last node
	{
		delete tmp;
		head = NULL;
	}

	else
	{
		Node *curr = tmp;
		tmp = tmp->getNext();
		//Search through nodes
		while (tmp != NULL)
		{
			if (tmp->getVal() == y)
				break;
			curr = tmp;
			tmp = tmp->getNext();
		}

		curr->setNext(tmp->getNext());
		delete tmp; //delete current node
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,236
  • Joined: 05-May 12

Re: error C4703: potentially uninitialized local pointer variable

Posted 09 February 2018 - 03:31 PM

Never mix C and C++ memory allocation functions. You are using new to allocate a block of memory, but using free() to deallocate the block.
Was This Post Helpful? 1
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: error C4703: potentially uninitialized local pointer variable

Posted 09 February 2018 - 03:32 PM

I would say that the error is probably prior to that line, you probably need to backtrace a little bit to find the actual source of the problem. I'd pay particular attention the those pointers, if you have a nullptr then trying to dereference the pointer will probably cause a crash.

Also why are you trying to define operator new and operator delete?

Jim

This post has been edited by jimblumberg: 09 February 2018 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 kiddjsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 06-June 17

Re: error C4703: potentially uninitialized local pointer variable

Posted 09 February 2018 - 03:58 PM

@Skydiver

Are you referring to these two lines of code here?

void *operator new(size_t size){ return new char[size]; }
void operator delete(void *ptr){ free(ptr); }



I have updated the code to:

void *operator new(size_t size){ return new char[size]; }
void operator delete(void *ptr){ delete(ptr); }



@Jim

I'm defining operator new and delete because the following syntax errors are received without the operators being defined:

[00:53:14] Error 1 error LNK2019: unresolved external symbol "public: static void * __cdecl Node: perator new(unsigned int)" ([email protected]@[email protected]) referenced in function "public: void __thiscall LinkedList::Append(int)" ([email protected]@@[email protected])
[00:53:23] Error 2 error LNK2019: unresolved external symbol "public: static void __cdecl Node: perator delete(void *)" ([email protected]@[email protected]) referenced in function __unwindfunclet$[email protected]@@[email protected]$0
[00:53:31] Error 3 error LNK1120: 2 unresolved externals 1 1 virtualMemory

This post has been edited by kiddjsh: 09 February 2018 - 03:59 PM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,236
  • Joined: 05-May 12

Re: error C4703: potentially uninitialized local pointer variable

Posted 09 February 2018 - 04:37 PM

The following compiled and linked without errors for me on VS2017:
#include <iostream>
using namespace std;


//looks like this:
// ---------              ---------
// -  x    -              -   x  -
// ---------              ---------
// -Pointer-  -------->   -Pointer-
// ---------              ----------
//
//Individual node class. Contains one integer x value and a pointer to the next node
class Node
{
private:
    int x;
    Node *next;
public:
    Node() {};
    Node(int y) { x = y; next = NULL; }
    static Node *freelist;
    void setVal(int data) { x = data; }
    void setNext(Node* aNext) { next = aNext; };
    int getVal() { return x; };
    Node *getNext() { return next; };
    static void printFreelist();
};

Node* Node::freelist = NULL;

//Linked List class. Contains head node which is initially set to null
class LinkedList
{
private:
    Node * head;
public:
    LinkedList() { head = NULL; };
    void Print();
    void Append(int y);
    void Delete(int y);
};

//Append a node to the list
void LinkedList::Append(int y)
{
    //Create a new node and set the integer data value
    Node* newNode = new Node();
    newNode->setVal(y);
    newNode->setNext(NULL);

    Node *tmp = head;//Create a temp pointer

                     //Search through the list
    if (tmp != NULL) {
        while (tmp->getNext() != NULL) {
            tmp = tmp->getNext();
        }
        // point the last node to the new node
        tmp->setNext(newNode);
    }
    else {
        //First node in the linked list
        head = newNode;
    }


}

//Delete a node from the list
void LinkedList::Delete(int y)
{
    Node *tmp = head; //Create a temp pointer
    if (tmp == NULL) //No nodes
        return;

    if (tmp->getVal() == y)
    {
        head = head->getNext();
        delete tmp;
    }

    if (tmp->getNext() == NULL) //Last node
    {
        delete tmp;
        head = NULL;
    }

    else
    {
        Node *curr = tmp;
        tmp = tmp->getNext();
        //Search through nodes
        while (tmp != NULL)
        {
            if (tmp->getVal() == y)
                break;
            curr = tmp;
            tmp = tmp->getNext();
        }

        curr->setNext(tmp->getNext());
        delete tmp; //delete current node
    }
}

void LinkedList::Print()
{
    Node *tmp = head;
    if (tmp == NULL)
    {
        cout << "Empty" << endl;
        return;
    }
    do {
        cout << tmp->getVal();
        cout << " -->";
        tmp = tmp->getNext();
    } while (tmp != NULL);
    cout << "NULL" << endl;
}

int main()
{
    LinkedList newLinkedList;
    newLinkedList.Append(42);
    newLinkedList.Print();
    newLinkedList.Append(17);
    newLinkedList.Print();
    newLinkedList.Append(21);
    newLinkedList.Print();
    newLinkedList.Append(1701);
    newLinkedList.Print();
    newLinkedList.Delete(1701);
    newLinkedList.Print();
    newLinkedList.Delete(17);
    newLinkedList.Print();
    newLinkedList.Delete(21);
    newLinkedList.Print();
    newLinkedList.Delete(42);
    newLinkedList.Print();
    newLinkedList.Append(42);
    newLinkedList.Print();
    newLinkedList.Append(17);
    newLinkedList.Print();
    newLinkedList.Delete(17);
    newLinkedList.Print();
    newLinkedList.Delete(42);
    system("pause");
}


Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,236
  • Joined: 05-May 12

Re: error C4703: potentially uninitialized local pointer variable

Posted 09 February 2018 - 04:45 PM

As for your crash, you are deleting a block of memory (line 79) and then accessing it shortly thereafter (line 82).
Was This Post Helpful? 0
  • +
  • -

#10 kiddjsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 06-June 17

Re: error C4703: potentially uninitialized local pointer variable

Posted 09 February 2018 - 05:52 PM

@Skydriver

Thanks, I commented out line 82, //delete tmp; and the Unhandled exception error isn't being thrown any longer.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,236
  • Joined: 05-May 12

Re: error C4703: potentially uninitialized local pointer variable

Posted 09 February 2018 - 06:08 PM

Sure, but now you are leaking memory if there is more than one node in the list.

Furthermore, you also will have a crash if you following the sequence:
Append 42
Append 17
Delete 42
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1