Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-21-2003, 06:00 AM   #1
LQ Newbie
Registered: Jul 2003
Location: Birmingham
Posts: 20

Rep: Reputation: 0
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.

Any suggestions will be greatly appreciated,
Old 07-21-2003, 06:49 AM   #2
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
its better to do a 2d array linearly instead of double pointers, have a look here:
Old 07-21-2003, 02:19 PM   #3
LQ Newbie
Registered: Jul 2003
Location: Birmingham
Posts: 20

Original Poster
Rep: Reputation: 0
correct method


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];


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Memory Allocation jvff Programming 1 09-05-2005 05:01 AM
dynamic memory allocation failure guam Programming 4 04-13-2005 09:16 AM
dynamic memory allocation in kernel module appas Programming 4 09-21-2004 06:36 AM
dynamic allocation of memory in kernel space appas Programming 1 08-04-2004 06:32 AM
pointers and dynamic memory allocation deveraux83 Programming 2 01-24-2004 10:35 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration