Adding vector initialization to structure constructor

I have a structure that looks like this:

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 results in a runtime error: 0xC0000005: Location of access violation entry 0xcdcdcdcd. Unhandled exception at 0x771115de in Octree_octnode_0113.exe: 0xC0000005: Location of access violation entry 0xcdcdcdcd.

An access violation occurs when an empty vector is created. Is there a way to initialize the vector in the constructor so that the error doesn't occur?

+3


source to share


3 answers


In the next

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

      

you are passing an uninitialized pointer to memset()

. Did you mean to say:

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

      



or simply,

  parent = NULL; // or nullptr

      

?

+3


source


This line uses an uninitialized pointer:

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

      

You should just install it instead NULL

:



parent = NULL;

      

(Or better yet, do it in the init list :)

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

      

+2


source


The 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?
    }
};

      

0


source







All Articles