c++ - How to create an array of pointers inside a function whose size is not known till run time -



c++ - How to create an array of pointers inside a function whose size is not known till run time -

in main can:

node* mynodearray2[myheight][mywidth];//does not phone call constructor for(int i=0; i<myheight; i++){ for(int j=0; j<mywidth; j++){ thenodearray[i][j] = new node("thisistest", 5, 5); } }

so above code myheight , mywidth can user input @ run time. not phone call default constructor , can utilize new operator , go through array creating objects.

i want able pass node* mynodearray2 function , allow create array size , populate it. when created want elements pointers. don't want phone call default constructor. want able @ choosing phone call new operator non-default constructor.

when try:

void test(node*& thenodearray, int myheight, int mywidth){ thenodearray = new node*[myheight][mywidth]; } int main(){ node* mynodearray; test(mynodearray, myheight, mywidth); }

i

"mywidth not constant expression."

i have tried couple of different methods cannot want. need creation happen in separate function. need able define size @ runtime. help?

edit:

i don't want utilize std::vector.

edit 2:

i don't want this

int** ary = new int*[sizex]; for(int = 0; < sizex; ++i) ary[i] = new int[sizey];

as forces rows of objects of contiguous memory space. want allocate 2d array of pointers. not want create objects pointed to.

you may utilize following:

node*** makearraynodeptr(int myheight, int mywidth){ node*** res = new node**[myheight]; (int = 0; != myheight; ++i) { res[i] = new node*[mywidth](); } homecoming res; }

and don't forget

void deletearraynodeptr(node*** nodes, int myheight, int mywidth) { (int = 0; != myheight; ++i) { // , probably: /* (int j = 0; j != mywidth; ++j) { delete nodes[i][j]; } */ delete [] nodes[i]; } delete [] nodes; }

c++ arrays function pointers multidimensional-array

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -