0

I can't seem to figure out why my program crashes every time I do anything to my Node object. And I can't make any progress until this is working...

My problem is: Whenever I try to "Set Data" on the Node X, I get an access violation error. Before that I was getting a Runtime failure #3 about not initializing the variable. So I initialize it to null, and I still get an error. My previous Node classes never gave me this error, so I am kinda stumped. Any help is appreciated!

Here's the code:

Node Class:

#ifndef NODE
#define NODE

template <typename T>
class Node
{
public:
    Node();
    Node(T data);
    void SetData(T data);
private:
    T m_Data;

};

template <typename T>
Node<T>::Node()
{

}

template <typename T>
void Node<T>::SetData(T data)
{
    m_Data = data;
}
#endif

Main:

#include <iostream>
#include <crtdbg.h> 
#include "Node.h"
#define _CRTDBG_MAP_ALLOC

int main()
{
     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

     Node<int> * x = nullptr;
     x->SetData(20);

     return 0;
}

The weird thing is, whenever I do a "New" to allocate a new node, this problem doesn't occur.. Thanks in advance.

Taylor
  • 123
  • 1
  • 1
  • 7
  • Okay I tried making it a local variable and it still crashed on me for not being initialized..Runtime check failure #3 I don't understand why this is happening now when my last 4-5 projects I could instantiate these all over with no problem?? – Taylor May 20 '13 at 03:03

2 Answers2

1

You are dereferencing a null pointer value which results in undefined behavior. You need to create an instance of Node<int> and assign it to x like so

Node<int> * x = new Node<int>();
x->SetData(20);

or use automatic storage duration and declare x by value.

Node<int> x;
x.SetData(20);
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • Okay, Thanks for your response! however, If I use the local variable option, I get a runtime error #3 for not initializing the variable.. – Taylor May 20 '13 at 02:55
  • The error sounds like you're declaring the local variable as `Node *x;` instead of `Node x;` – Captain Obvlious May 20 '13 at 03:02
  • Yes you're right I am. So you're saying I cannot instantiate a Node pointer without allocating new memory? I would rather not to avoid memory leaks.. – Taylor May 20 '13 at 03:07
  • If you declare `x` as a pointer you need to create an instance and assign it to `x` before you use it. IF you want to avoid memory leaks use smart pointers like `std::unique_ptr> x`. – Captain Obvlious May 20 '13 at 03:10
  • Okay, I see why now. Thank you very much for your help Captain Oblivious, very helpful! – Taylor May 20 '13 at 03:14
0
  Node<int> * x = nullptr;
  x->SetData(20);

x is a null pointer, you use null pointer to access member function of your class that changes data of your class, it will result in undefined behavior. See what will happen when I call member function on NULL object pointer for more information.

You should initialize the pointer to some objects of your class:

  Node<int> * x = new Node<int>();
  x->SetData(20);
Community
  • 1
  • 1
taocp
  • 23,276
  • 10
  • 49
  • 62