-4

I keep getting the following error: Unhandled exception at 0x009f240e in OpenGL and GLUT - 101.exe: 0xC0000005: Access violation writing location 0x00000000.

I have found some questions here with a similar error code but they all seem to say the problem is: dereferencing a NULL pointer. However, I can't seem to find anything that really explains what that means or what to do about it.

Here is a small sample of my code:

Tree* myTree; //global variable pointer

int main (int argc, char** argv)
{   
      fstream file = fstream ("Tree.txt");

      *myTree = Tree(file);   // This is where the error is occuring
}

If anyone has any advice or ideas on what I could do to fix this, please let me know. If there is any other code I should add, let me know that as well.

user1824239
  • 167
  • 1
  • 1
  • 5
  • Well, check if `file` is opened/valid. – Kiril Kirov Nov 14 '12 at 16:16
  • @KirilKirov, the problem is the `*myTree`. – hmjd Nov 14 '12 at 16:17
  • 3
    Please, please, please. Consider reading at least a basic C and/or C++ book before asking questions like this. You dereference a null pointer, it is obvious what the problem is even from error message you get, you even add a null-pointer tag, yet still you ask it here... –  Nov 14 '12 at 16:18
  • @Vlad: It's not luck, global variables are guaranteed to be initialized to 0 at program start. – Adam Rosenfield Nov 14 '12 at 16:19
  • @AdamRosenfield: You are right, corrected. But the point still remains :) –  Nov 14 '12 at 16:20

2 Answers2

2

This:

*myTree = Tree(file);

is writing to un-allocated memory, by dereferencing an uninitialized pointer. It seems the pointer points to 0, which explains the exception at that address.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • `myTree` is not uninitialized -- global variables are always guaranteed to be initialized to 0 at program start. – Adam Rosenfield Nov 14 '12 at 16:18
  • I understand that the issue is that I am trying to dereference a null pointer. What I don't understand is what this means or how to allocate memory for a pointer of an object. – user1824239 Nov 14 '12 at 16:26
  • @AdamRosenfield Isn't that only true for `static` variables? – unwind Nov 14 '12 at 16:30
  • @unwind: No, all objects at global scope are zero-initialized, regardless of whether they have internal linkage (`static`) or external linkage (`extern`). See C++03 §3.6.2/1 (Initialization of non-local objects) `[basic.start.init]`. – Adam Rosenfield Nov 14 '12 at 18:26
0

I will guess that what you are trying to do is create an instance of Tree and have the global pointer myTree point to it.

If that is the case, your problem is in the syntax you use to create and assign that instance. *myTree actually dereferences the pointer, and you try to assign it to a copy of Tree(file)

What you need to do is create an instance of Tree with the new operator, and assign the return value (which is a pointer to the newly created instance) to the pointer myTree (as opposed to the dereferenced *myTree).

int main (int argc, char** argv)
{   
      fstream file = fstream ("Tree.txt");

      myTree = new Tree(file);   // Create a new instance of Tree and store its pointer in myTree
}

Don't forget to free the memory created by the operator new by calling delete on myTree when you're done with it.

Community
  • 1
  • 1
Fred
  • 4,894
  • 1
  • 31
  • 48