Jump to content
  • Advertisement
Sign in to follow this  
OpenGL_Guru

multi-dimensional dynamic arrays....

This topic is 4805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all -- i have some data that i have sorted out into a file for easier reading and i will store the data into structs, each struct contains 5 elements. however i wont know how many i will need until after the program is running. so i can say something like mystruct *mydata; each section of the data file has a different number of struct to allocate. so i can say in the the class.cpp something like mydata = new mystruct[num_points]; ok thats fine and dandy. i am actually going to need to have an array of mydata. i wont know how many until the program is running. how do i set up a dynamic array of type mydata?? ive done some reading and some have said do the following... mydata *m; -- but this doesnt compile in the class header, so i guess i need something at run time in my program... mydata = new mystruct[num_points]; m = new mydata[num_sections]; is this the right idea??

Share this post


Link to post
Share on other sites
Advertisement
I think you are on the right track, but have some interfering problems. For the specified code to compile, make sure that the type you use for the array is defined beforehand. Just a quick example:

struct SDataType
{
// TODO: describe data
};

// Declare the array
SDataType** ppData;

// Get the number of 'sections'
unsigned int nNumSections = ...;
ppData = new SDataType*[nNumSections ];

// Iterate through the sections
for ( unsigned int nSection = 0; nSection < nNumSections ; nSection++ )
{
// Get the number of 'points'
unsigned int nNumPoints = ...;
ppData[nPoint] = new SDataType[nNumPoints ];

// Read data into array
for ( unsigned int nPoint= 0; nPoint< nNumPoints ; nPoint++ )
... ppData[nSection ][nPoint] ...
}

// When done delete
for ( nSection = 0; nSection < nNumSections ; nSection++ )
delete[] ppData[nSection];
delete[] ppData;


Hope it helps a little. Greetz,

Illco

[Edited by - Illco on June 6, 2005 11:22:57 AM]

Share this post


Link to post
Share on other sites
well let me explain more clearly to see if we are on the same page.

in my class header i have my struct defined called mystruct lets say with 5 elements.

then i want to create array of these. so following your example:

mystruct *ppdata; //do we need double referencing with an extra *??
now i also in my class header need to declare something called mysections and for its type to be of type ppdata. at the time of these declarations i dont know the number of sections. i wont know until the program runs and reads from the file.

so in section 1 there might be 60 points. each point will have 5 elements to it via the struct. section 2 might have 35 points.

//so in my class .cpp file i need to allocate these:

infile >> sections;
mysections = new ppdata[sections];

so in my loop in my class.cpp i need to say something like..



for(int i = 0; i < sections; i++)
{
infile >> num_points;
ppdata = new struct_data[num_points];
for(int j = 0; j < num_points; j++)
infile >> mysections[j].x >> mysections[j].y >> mysections[j].z;

}






i guess i was a little confused by your code or maybe you misunderstood my OP. maybe we are on the same page. can you tell me what you think? i appreciate your help Illco. :)

Share this post


Link to post
Share on other sites
I think we're on the same page, except for that I reversed 'points' and 'sections'. I had points containing a number of sections, while it is vice versa. Yes, you need the double pointer because you have an array of arrays. Each 'section' is represented as an array of points. I've fixed the order in my previous post, and as far as I get it does precisely what you asked.

To alter your code a bit, you need to allocate room for the points for each section separately. So it becomes:

for(int i = 0; i < sections; i++)
{
infile >> num_points;
ppdata = new struct_data[num_points];
for(int j = 0; j < num_points; j++)
infile >> mysections[j].x >> mysections[j].y >> mysections[j].z;

}


Illco

Share this post


Link to post
Share on other sites
Quote:
Original post by OpenGL_Guru
mydata = new mystruct[num_points];
m = new mydata[num_sections];

is this the right idea??


I think your very confused [lol], are you sure you know the difference between a type an instance of a type?

Quote:
Original post by Illco

// Declare the array
SDataType** ppData;

// Get the number of 'sections'
unsigned int nNumSections = ...;
ppData = new SDataType[nNumSections ];
//....



Whoopsie-daisy i think you have a typo here [wink].

Share this post


Link to post
Share on other sites
so in my class header i now have...

mystruct **ppdata;




in my class .cpp when i read in the first point.. i say

ppdata[0] = new mystruct[points];

why do i need to allocate the room for each section? i thought thats what num_sections was for.. speaking of which in the class header how am i going to define num_sections? sorry for me being confused. thanks Illco. and yeah i think i was thinking of typedefs snk_kid :)

Share this post


Link to post
Share on other sites
Ok. As I understood your data layout is as follows: you have a number of sections (lets say N sections) and each sections has a number of points (lets say M(N) points; where M(N) is different for each section). Then what you are aiming for is a structure like:

Section 1
Point 1
Point 2
...
Point M(1)
Section 2
Point 1
Point 2
...
Point M(2)
...
Section N
Point 1
Point 2
...
Point M(N)

So at the top level you have an array of N sections. Now if each section would contain just precisely one point, then you could do:

SData* pSections = new SData[nNumSections];
for ( i = 0; i < NUM_SECTIONS; i++ )
pSections = point_something;

However this is not the case; each section contains yet another array of points. This second array does not have a fixed size for each section; it differs per section. So much like we created an array of N sections, now we need for each section to create an array of M(i) points:

SData** ppSections = new SData*[nNumSection]; /* Waves at snk_kid */
for ( i = 0; i < M; i++ )
{
// ppSections is yet another array
ppSections = new SData[nNumberOfPointsInTHISSection];

for ( j = 0; j < M(i); j++ )
ppSections[j] = point_something;
}

Now I think all code is in place; it already was. You should get this to work or reread the C++ book for better understanding.

As for the number of sections variable, you can just put it in the class. Only you don't immediately allocate the array or its value.

Greetz,

Illco

Share this post


Link to post
Share on other sites
an example of the data file.
25000

60
d x y z
d x y z
....
....58 more times

35
d x y z
d x y z
....
....33 more times

100
d x y z
...99 more times

...until the end of file.


infile >> sections; //number of sections in file -- this is static of course
for(int i = 0; i < sections; i++)
{
infile >> points;
ppdata = new Sdata[points]; //ppdata was declared in the class as Sdata **ppdata; NOTE -- so how do we declare
//if i say ppdata = new Sdata*[points] i get a compiler error.
for(int j = 0; j < points; j++)
infile >> ppdata[j].d >> ppdata[j].x >> ppdata[j].y >> ppdata[j].z;

}





if i am doing to do it this way no need to declare a variable of type ppdata?? im just not used to seeing double referencing so its got me a bit confused. thanks for your time and patience Illco.

Share this post


Link to post
Share on other sites
No mention of STL yet? Amazing [wink]

I don't know if this would confuse you more, but I find it easier to manage "pretend" double-arrays:

array = new struct_data[num_sections * num_points];

Now the ugly part. To access an index, instead of array[section][point], you have array[section*num_points + point]. Wrapping it in an interface function makes it pretty legible.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!