arrays - Dynamic Memory Management for a pointer to a pointer (c++) -


need help. have array of pointers:
int *ptr = new int[n]; 
want 2 dimensional array of pointers, points 1 dimensional array. know how allocate memory 2 dimensional pointer:
int (*nn)[4] = new int [n][4]; 

but: how allocate memory 2 dimensional pointer pointer? possible anyway?

need define neighbors on grid. connection should this:
for (int vertex = 0; vertex < n; ++vertex) {     nn[vertex][0] = ptr[(vertex + 1) % n];     nn[vertex][1] = ptr[(vertex + n - 1)% n];     nn[vertex][2] = ptr[(vertex + l) % n];     nn[vertex][3] = ptr[(vertex + n - l) % n];     if (vertex % l == 0) {         nn[vertex][1] = ptr[vertex + l - 1];     }     if((vertex + 1)%l == 0){         nn[vertex][0] = ptr[vertex - l + 1];     } } 

i got stuck in allocating memory...

if want 2 dimensional array of pointers int, need pointer pointer pointer int, , need allocate memory in for-loop, this:

int *** nn = new int**[n]; for(int i=0; i<n; i++) nn[i] = new int*[4]; 

now nn[x][y] pointer int , *(nn[x][y]) int.

it true comments say, though, ptr not array of pointers. however, can still make nn point values of ptr doing this:

nn[x][y] = ptr+index; 

and not:

nn[x][y] = ptr[index]; 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -