LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Dynamic Memory Allocation query (https://www.linuxquestions.org/questions/programming-9/dynamic-memory-allocation-query-74526/)

dhanakom 07-21-2003 06:00 AM

Dynamic Memory Allocation query
 
Hello all,

The syntax that I used for dynamically allocating a 2D array is

float (*points_) [3] = new float[npoints_][3];

This approach worked quite well, unless I had a need to make points_ as my class variable.

I declared float **points_ as a private data memeber of my class
in the constructor when I'am trying to allocate the 2D array, I cannot figure out what could be the syntax, as I uderstand I need to define points_ as a pointer to an array of 3 elements but (*points_)[3] = new float[npoints_][3] returns an error message.

Even though dynamic allocation is fun, it sucks not to know the synatx. :D

Any suggestions will be greatly appreciated,

kev82 07-21-2003 06:49 AM

its better to do a 2d array linearly instead of double pointers, have a look here: http://www.linuxquestions.org/questi...383#post363383

dhanakom 07-21-2003 02:19 PM

correct method
 
kev82,

Thanks for your thoughts.

I' am unable to use the first approach of new[w*h], because that would require extensive changes to my program.

I figured out my mistake and fixed it and the program runs fine. Please comment on the below approach, which I' am using in my program.

//in the header file
float (*points_)[3];

//in the member methods where I need to allocate
points_ = new float[npts_][3];


All times are GMT -5. The time now is 06:08 AM.