Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding vector initialization to struct constructor

Tags:

c++

struct

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?

like image 660
shunyo Avatar asked Dec 09 '25 20:12

shunyo


2 Answers

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

?

like image 155
NPE Avatar answered Dec 12 '25 08:12

NPE


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));
}
like image 39
cdhowie Avatar answered Dec 12 '25 09:12

cdhowie