0

All I am doing is declaring a pointer and then checking if its pointing to anything:

#pragma once

#include <Node.h>
#include <stdio.h>

template <class NodeDataType>
class LinkedList
{
typedef Node<NodeDataType>* pNode;

private:    
pNode pHead;


pNode pTail;



public:

void Add(NodeDataType* pNodeTypeData)
{


    if(pHead)
    {
        printf("phead is initialized\n");
    }
    else
    {
        printf("phead is not initialized\n");

    }               
}


};

then, in main(), I am making a new LinkedList object, and calling Add but when i run it, it outputs pHead is initialized ? but i never initialized it?

can someone explain it to me?

thanks

(main)

#include <stdio.h>
#include <LinkedList.h>
#include <GameObject.h>

int main(int argc, char** argv)
{

LinkedList<GameObject> meList;


meList.Add(new GameObject());


return 0;
}
  • There can be several things wrong here, please post the code in main also so that we can see the error for ourselves – Curious Jun 15 '17 at 08:24
  • 2
    look at this https://stackoverflow.com/questions/1910832/why-arent-pointers-initialized-with-null-by-default – Andrew Kashpur Jun 15 '17 at 08:25
  • Does it ouputs `initialized` or `uninitialized`. Because you never initialize `pHead` but checking it in `if (pHead)`. – Andre Kampling Jun 15 '17 at 08:32
  • it outputs initialized, even though i never did. –  Jun 15 '17 at 08:34
  • Could you please copy the whole output. – Andre Kampling Jun 15 '17 at 08:34
  • just outputs: phead is initialized. nothing else, but i after reading what andrew linked it seems to be because c++ apparently just doesn't initialize pointers to null when they are declared, so ill have to do it myself i guess –  Jun 15 '17 at 08:35
  • Ah sorry I thought you are talking about compiler output, I didn't read it accurately... Now I see it is your output... If you do **not** initialize variables they are uninitialized which means they contain the value that lie at the memory address where the variable lie. It does **not** get overridden by zeroes. This applies for POD types because they don't have a standard constructor like classes. The comment of Andrew Kashpur is pointing also in that direction! – Andre Kampling Jun 15 '17 at 08:38

1 Answers1

1

You do not initialise pHead or pTail in constructor or in place. So, these members will have arbitrary values when you create a LinkedList object. So, either write the constructor:

LinkedList()
    :pHead(NULL), pTail(NULL)
{
}

Or initialize them in-place if you are using c++11:

private:    
    pNode pHead = nullptr;
    pNode pTail = nullptr;
nakiya
  • 14,063
  • 21
  • 79
  • 118
  • I would also use `nullptr` in the initializer list `:pHead(nullptr), pTail(nullptr)`. – Andre Kampling Jun 15 '17 at 08:43
  • @AndreKampling : If OP is using C++11, yes. – nakiya Jun 15 '17 at 08:44
  • 1
    Yes sure you're right but using at least the `NULL` macro would be more clear then. But he could also provide an own `nullptr` type [see here at SO](https://stackoverflow.com/a/44517878/8051589) if his compiler doesn't support it. – Andre Kampling Jun 15 '17 at 08:46