c++ - How to allocate 2D array in a function -


i have function this:

void quadtree::alloc( quad***& pquadsarray ) {     const int _quadscount = 100;      // allocates memory 1 chunk of memory     quad** _data = new quad*[_quadscount * _quadscount];      pquadsarray = new quad**[_quadscount];     for( int = 0; < _quadscount; ++i ) {             pquadsarray[i] = _data + * _quadscount;     } }  // calling this: quad*** test = nullptr; alloc( test ); 

it works well. 1 doesn't , don't know why:

void quadtree::alloc( quad**** pquadsarray ) {     const int _quadscount = 100;      // allocates memory 1 chunk of memory     quad** _data = new quad*[_quadscount * _quadscount];      *pquadsarray = new quad**[_quadscount];     for( int = 0; < _quadscount; ++i ) {             *pquadsarray[i] = _data + * _quadscount; // code crashes here             // tried *(pquadsarray[i]) didn't     } }  // calling this: quad*** test = nullptr; alloc( &test ); 

what's wrong here?

you have operator precedence problem - change:

        *pquadsarray[i] = _data + * _quadscount; // code crashes here 

to:

        (*pquadsarray)[i] = _data + * _quadscount; 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -