3

I have a struct which is as follows:

struct octNode{
octNode* parent;
octNode* child[8];
std::vector<int> pointIndex;
//constructor of the struct
octNode()
{ 
      memset(parent,0,sizeof(parent));
      memset(child,0,sizeof(child));
}
};

But this throws a run-time error: 0xC0000005: Access violation writing location 0xcdcdcdcd. Unhandled exception at 0x771115de in Octree_octnode_0113.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.

The access violation occurs in the creation of the empty vector. Is there a way to initialize the vector in the constructor so that the error doesnt occur?

1
  • 4
    parent isn't pointing anywhere and it isn't initialized. How did you expect this to make sense? Commented Jan 24, 2013 at 15:50

3 Answers 3

3

In the following

  memset(parent,0,sizeof(parent));

you are passing an uninitialized pointer to memset(). Did you mean to say:

  memset(&parent, 0, sizeof(parent));

or, quite simply

  parent = NULL; // or nullptr

?

Sign up to request clarification or add additional context in comments.

1 Comment

The line for child is correct in the OP's code, since that's an array of pointers. Most likely, the initialization of parent should just be setting it to NULL (which can be done in the initialization list).
2

This line causes the use of an uninitialized pointer:

memset(parent,0,sizeof(parent));

You should just set it to NULL instead:

parent = NULL;

(Or better yet, do so in the initialization list:)

octNode() : parent(NULL)
{ 
      memset(child,0,sizeof(child));
}

5 Comments

why no initializer list for child?
@MooingDuck To my knowledge, an array cannot have its elements initialized in an initializer list prior to C++11. Plus, that would be child{NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL} which is verbose and fragile (changing the length of the child array would render the initialization incomplete). Of course, an std::vector would be better than an array.
:child() works in C++03 and C++11 both. Exactly the same as new T[2] is default-initialized, but new T[2]() is value-initialized: ideone.com/3Paskj
@MooingDuck Then that would work too, yes. (However, the answer to the question is "you are initializing parent wrong," so in that respect the initialization style of child is noteworthy, but not directly related.)
Yeah, it's not directly related to the question, but I thought it was noteworthy as well.
0

Code should say:

struct octNode{
    octNode* parent;
    octNode* child[8];
    std::vector<int> pointIndex;
    //constructor of the struct
    octNode()
    : parent(NULL) // maybe nullptr ?
    , pointIndex()
    { 
        std::fill_n(child, 8, NULL); // nullptr?
    }
};

1 Comment

child can also be default constructed like pointindex, it will fill the array with null values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.