Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Caesar

Dynamically allocated arrays

This topic is 6152 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

Is there any other way to do dynamically allocated arrays than to do those link-list (each entity contains pointer to the next one)? How do you solve such problems in your applications? Thanks a lot

Share this post


Link to post
Share on other sites
Advertisement
I think I know what you mean. What I would do if I wanted a dynamically created array is write code like this:

int *newArrayInt;

newArrayInt = (int *)malloc(array_size * sizeof(int));

then you will be able to use this array of memory decalred for use and free it by doing this

free (newArrayInt);

Simple.. But what is more simple.. if the array you intend on using is local in a function and at the time of execution you want a new array then if you know the size then you can just do this

size=574776;

int newArrayInt[size];

which would do the same as above.

I am not sure If I answered your question properly... I hope I did

Dark Star
UK

Share this post


Link to post
Share on other sites
No, this is not my problem. Assume you have a structure containing some information. And I need to have an array containing any amount of these structure (that''s what you''ve explained) but I also need to add new entities to it anytime. I may also create a new array of the desired size and copy the previous to it and then add the new entities, but that''s not an exellent solution anyway.

Share this post


Link to post
Share on other sites
I think I understand what you want. You could use the realloc function. Just pass the pointer to the memory you allocated and the new size, and it will just allocate more memory in the same space, or if it can''t it will find a new area of memory and copy the old data into it. That way you can change the size of your array at any time and keep all the contents of it intact so you can add more data to it.

I hope this is what you were asking for, if not explain it again.

Share this post


Link to post
Share on other sites
Hi Caesar...

Well noparity had a good idea about using the vector. And a linked list probably wouldn''t be too bad either.

But basically there is no way to dynamically grow an actual array without reallocating and copying. That is basically what realloc does and you could probably do it more efficiently yourself.

Personally I would use a vector, or if the information needed to be accessed faster than a vector would allow I would just make my initial array as big as I will ever need it and just keep adding to it. As long as I don''t expect it to be too horrendously big. A few hundred-thousand elements would probably be my limit. Memory is cheap these days. Reallocating and copying on the fly everytime you needed to add an element would murder your performance. There aren''t alot of operations that are slower than memory allocation

Another option would be that when you reallocate don''t just reallocate for a single new element. Set your array at a reasonable size at first, if you reach that limit reallocate enough memory to store maybe 100 or 1000 more elements. This way you only have to reallocate once in a while. But if you start getting a large number of elements, copying to the new memory will still be slow.

Seeya
Krippy

Share this post


Link to post
Share on other sites
I think I''ll stay with my linked list. Thanks. But I''m still interested what are those vectors. Can someone explain that to me? Again, thanks to anyone who have answered...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
To use integer STL vectors:

#include

std::vectorintvector;

intvector.pushback(5);

// Now you have placed a new int value of 5 last in the list.

// You can also use the vector like a ordinary array
intvector[0] = 2;

// And you can get the last element in the list very easy and doesn´t need to have a variable that tells you how big the array is.
int lastvalue = intvector.front();

Share this post


Link to post
Share on other sites
STL's vector or list is the way you want to go. I know you're hung up on using a linked list, but my impression is that it doesn't matter how your items are all collected and handled, as long as you can access them similarly to a regular array but with the ability to add items dynamically.

Both vector and list have the same basic functions. For example, to find out how many items the object is holding, call size. To add an item to the end, call push_back. vector allows access with an array-style index, while list requires that you use iterators.

Internally, vector usually uses the realloc approach Midnight Coder mentioned. So, random access is extremely fast--as fast as with a regular array--but resizing is slow. If you know how many elements you'll need (like, say you have 5 now, and you know you're going to add 3 more), you can use the reserve function to reserve the necessary space to avoid the internal reallocation (you'd reserve space for 8 items in this case)--what this does is to grow the container, preserving all current items.

On the other hand, list is usually implemented as a linked list (relax about the linked lists, okay? ). This means that resizing is extremely fast, but random access isn't and there's nothing that can be done about that.

If I were you, I'd use vector--it's exactly a dynamic array...and much more.

Edited by - merlin9x9 on July 18, 2001 5:23:29 AM

Share this post


Link to post
Share on other sites
Fine, I''ll try both linked lists and vectors.

Btw. If I remember right, I may overload the [] to make the linked list accesable like an array, or not?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!