Skip to main content
gamedev.net
🔒 Locked

Varible sized array.

Started by Jason2Jason 11 replies 1k views
Original Post
Jason2Jason
Jason2Jason
Would it be better to go about changing the size of a array by using realloc(), or creating a linked list. I dont need any special sorting functions, but i do need to know how may items there are in the array. Would i do this by:
CSomeClass* SC;

...

SC = malloc(sizeof(CSomeClass)*5);         //5 Classes..

int num_items = sizeof SC[];
...

SC = realloc(SC, (sizeof(CSomeClass)*5)+num_items);    //Add 5 more to array

...

num_items = sizeof SC[];
  
If thats wrong, how do you measure the real size of a dynamicly allocated array? Thx, -J [edited by - jason2jason on January 25, 2003 11:24:14 AM]
AndyOxfeld
AndyOxfeld
I don''t think using sizeof will work in that case. A better method would be to have another variable, an int, where you would store the size of the array. Whenever you change the size of the array, you change the size of the int.

Also, I believe your realloc line is wrong. If you want to add 5 more to the array, you should use this line:

SC = realloc(SC, sizeof(CSomeClass)*10);

The reason it should be *10 and not *5 is because you also need to reallocate the original 5, plus the new 5.

Next, whether to use an array with realloc, or a linked list. There are several advantages and disadvantages to each. Standard arrays are much easier to use. You can also directly index the Nth member using array[N], which you can''t do using linked lists. Arrays also use less RAM. However, it is MUCH easier (and much faster) to insert/delete items from the middle of a linked list.

- Andy Oxfeld
Brother Bob
Brother Bob
The sizeof operator returns the size of the datatype you pass, and if you pass a pointer to something you get the size of that pointer, which is usually 4 bytes. You can''t, without using compiler specific functions, get the size of the block a pointer points to, and not even sure all compiler provides such function. As Andy mentioned, track the size with a variable; after all, you must know the size in order to allocate enough memory.
Tuesdays Gone
Tuesdays Gone
That would definatly not work.

if you want to save yourself the headache, use an STL vector.
it has functions for handling everything you described.
Miserable
Miserable
Strange question - if you are using a linked list, you are clearly not using an array. They are distinct structures with different pros and cons - arrays have fast lookup; fast insertion and slow search (if unordered) or slow insertion and fast search (if unordered); they are difficult to resize (and it''s generally a slow operation); insertion in the middle and deletion anywhere but at the end is slow. Linked lists have slow lookup, fast insertion anywhere but slow search, and grow dynamically. Which do you need?

By the way, since you appear to be using C++, the best thing to do would probably be to use a vector, which will handle the resizing for you without bogging you down in low-level implementation details. As for the number of items, you can simply call the size method.
Jason2Jason
Jason2Jason
OK, i'll go into a little more info on what i need to do.

I'm creating a GUI system for a game. There will be a managing class, and a group of classes that represent each type of controll (EG: button, text, combo-box), and the control class needs to hold a array/linked list of each type of control. This will change during the game, but mostly at the begginning of a level, or when a menu or diologe box loads.

What would you recommend?

-J

[edited by - jason2jason on January 25, 2003 11:59:08 AM]
Evil Bill
Evil Bill
Take a look at std::vector. Its basically an array that you can resize. Its just as fast to look up an element in a vector as it is in an array. vectors take up slightly more RAM than normal arrays, because if you ask it to allocate space for 5 elements, it may allocate 10, but onlt use 5. That way, when you come to add an element, you don''t need to reallocate memory.

Since its unlikely you will be adding /removing loads of controls per frame (you''d only notice the speed difference if you were doing several realloc()s per frame), i''d go for either a linked list or a vector. Arrays probably aren''t worth the hastle.

HTH

Member of the Unban Mindwipe Society (UMWS)
AndyOxfeld
AndyOxfeld
quote:
Original post by Jason2Jason
OK, i'll go into a little more info on what i need to do.

I'm creating a GUI system for a game. There will be a managing class, and a group of classes that represent each type of controll (EG: button, text, combo-box), and the control class needs to hold a array/linked list of each type of control. This will change during the game, but mostly at the begginning of a level, or when a menu or diologe box loads.

What would you recommend?

-J

[edited by - jason2jason on January 25, 2003 11:59:08 AM]


For this situation, it'd probably be best to figure out the maximum number of each class you'll ever need, and store it in a #define, like so:
#define MAXBUTTONS 10  

Then make an array of pointers to the class:
class CMyButton* UIbuttons[MAXBUTTONS];
memset(UIbuttons, 0, sizeof(class CMyButton*) * MAXBUTTONS);

The memset is important, because it initializes all the pointers to NULL. Now you can add a button by looping through the array and finding the first NULL button:

int j;
for (j = 0; j < MAXBUTTONS; j++)
{
if (!UIbuttons[j])
{
UIbuttons[j] = new CMyButton;
break;
}
}

And to remove a button, just do this:

delete UIbuttons[3];
UIbuttons[3] = NULL;

Don't forget to set it to NULL.

That method is much simpler and faster than any of the other methods mentioned (resizable arrays using realloc, linked lists, and STL vectors).

- Andy Oxfeld



[edited by - AndyOxfeld on January 25, 2003 12:17:06 PM]
eldee
eldee
why not use a vector?
it''d make your life much easier...

-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

Miserable
Miserable
quote:
Original post by AndyOxfeld
For this situation, it''d probably be best to figure out the maximum number of each class you''ll ever need, and store it in a #define, like so:
#define MAXBUTTONS 10   



Heresy! Heresy!

Seriously, though, most people would normally recommend something like const int MAXBUTTONS = 10; over a #define; I see no reason not to prefer ''const variables''.
Jason2Jason
Jason2Jason
OK thanks guys, i''ll go with the max i''ll ever need option, since a pointer doesn''t take up much room. And yes i''d go with const int as well.

Thanks for all the ideas guys!

-J
evillive2
evillive2
how about using a linked list and global counter valriable and whenever you add or remove an item you increment/decrement the global counter then to get the size you can use

int bytes = global_counter*sizeof( CSomeClass );

as for searching through linked lists, especially if they are going to have hundreds or thousands of items, I break it up into smaller lists to search through and make sure each item has a unique ID number. I am not familiar with all the lingo, but I think this is kind of like hashing?

#define MAX_KEY 128
CSomeClass *class_array[MAX_KEY];

when I add another item to the list I do something like:
CSomeClass *item = new CSomeClass;
global_counter++;
item->ID = last_id++;
item->next = class_array[item->ID % MAX_KEY];
class_array[item->ID % MAX_KEY] = item;

then when I want to look for the item ID I use something like
CSomeClass *item_lookup( int ID )
{
CSomeClass *item;

for ( item = class_array[ID % MAX_KEY]; item; item = item-next )
if ( item->ID == ID )
return item;

return NULL;
}

of course this works for me, I like the kind of middle ground in lookup speed between an array and linked list I get, and I get the ease of inserting/deleteing from a linked list. Its what works for me.




[edited by - evillive2 on January 25, 2003 2:10:52 PM]
Evillive2
Jason2Jason
Jason2Jason
Well, i think i''ll stick with an array of pointers, i''ve got a way to keep track of it all worked out. Because the GUI will be used quite alot, i''m gonna need speed, so that the game isn''t slowed by working through linked lists where they aren''t necceserily needed. I think i''m gonna look into multi-threading too so that i can increase performance.

-J

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.