View Full Version : Dynamic array of objects in c++
Stowelly
05-20-2004, 07:29 PM
ok i have a program which loads a file into it i want to create an array of objects based on data loaded from this file.... is it possible
thanks
Jason
05-20-2004, 08:28 PM
I can't exaclty remember but Im almost positive that you can't make dynamic arrays in C++, you need java for that. And Im fairly certain that you can't even use a variable in the declaration of an array.
Jason
It is possible if you can find the size of the file. The easiest way to find the size of the file is to seek to end, then take the difference of end and start.
once you have the file size, just allocate memory required for the resultant objects..
The code would look like this
MyClass *MyObjectArray;
dwNumObjects = dwFileSize/sizeof(MyClass);
MyObjectArray = new MyClass[dwNumObjects];
// now you can use the object array as you wish
MyObjectArray[0].init(1302);
//dont forget to free the memory when you are done.
delete [] MyObjectArray;
shmoove
05-20-2004, 09:42 PM
You can also add a header with the number of objects to the beginning of the file. Specially if the objects might not all be the same size (ie, if they contain strings).
shmoove
Hmm, I have never considered varying size of objects created from same class. Can you explain how that might happen?
Here is how i see it in case of strings.
To have a dynamic length of string, we have to use a pointer which takes constant amount of space no matter how long the string is. The string itself is stored outside the object and has to be allocated, freed seperately from the object, likely in its constructor and destructors.
To have a static string of length X, the length is X no matter how much of it we use..
This might turn in to a informative lesson for me :thumbsup:
Stowelly
05-21-2004, 02:50 AM
It is possible if you can find the size of the file. The easiest way to find the size of the file is to seek to end, then take the difference of end and start.
once you have the file size, just allocate memory required for the resultant objects..
The code would look like this
MyClass *MyObjectArray;
dwNumObjects = dwFileSize/sizeof(MyClass);
MyObjectArray = new MyClass[dwNumObjects];
// now you can use the object array as you wish
MyObjectArray.init(1302);
//dont forget to free the memory when you are done.
delete [] MyObjectArray;
erm its not based on the size of the file... its based the result of calculating two numbers at the begining of the file called height and width. the data is already extracted into a carray. its basically for a tile map and need to create an array of pointers to each tile if that makes sense
My example should still hold good since all you need to know is how many elements the array has.
corrected a mistake above :o
Stowelly
05-21-2004, 03:56 AM
thanks alot mate ill have ago at that in the morning 3.00 in the uk
Stowelly
05-21-2004, 04:15 AM
hmm any idea why this isnt working
MapReader bob;
bob.LoadMap("level1.map");
int h = bob.getHeight();
int w = bob.getWidth();
int total = h * w;
cGetTwoCritterMap *List;
List = new List[total];
I think its because of this
List = new List[total];
should be
List = new cGetTwoCritterMap[total];
Stowelly
05-21-2004, 05:14 PM
damm my stupidity thanks alot
Stowelly
05-25-2004, 08:20 PM
aghh sorry one more problem with this... how do i create this dynamic array but then add the objects to it in a loop like so
int sprite = 0;
float xpos;
int ypos;
for (int i = 0; i <w;i++)
{
for (int j = 0; j <h;j++)
{
sprite = bob.GetCellContent(i, h-1-j);
if ((sprite > 0)&&(sprite <6))
{
xpos = i * Blockwidth + 0.5;
ypos = j;
add(new cGetTwoCritterMap(this,sprite, xpos, ypos));
}
}
with the line underneath being the one which needs to be added to the dynamic array
add(new cGetTwoCritterMap(this,sprite, xpos, ypos));
something like this?
int sprite = 0;
float xpos;
int ypos;
int count=0;
cGetTwoCritterMap **List;
List=new (cGetTwoCritterMap *)[dwNumObjects];
for (int i = 0; i <w;i++)
{
for (int j = 0; j <h;j++)
{
sprite = bob.GetCellContent(i, h-1-j);
if ((sprite > 0)&&(sprite <6))
{
xpos = i * Blockwidth + 0.5;
ypos = j;
List[count]=new cGetTwoCritterMap(this,sprite, xpos, ypos);
count++;
}
}
dont forget to free the memory!
for(i=0;i<count;i++)
delete List[i];
delete [] List;
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.