Arrays vs. Linked lists

Started by
12 comments, last by Forcas 22 years, 8 months ago
When using arrays and each time one becomes full, you could alloc certain number new elements (for example, 10 for sprite list) to overcome new/delete overhead problem (so new memory allocation is not necessarily needed every time new entry is added to the list). This way, both memory usage and new/delete overhead stay consistent.
Advertisement
quote:Original post by Magmai Kai Holmlor
Linked list are less efficeint in thier memory use than arrays are, but if you are considering a list, that implies you do not know for certain the maximum number of elements. In order to be safe with an array you need to allocate more space than is needed, a list will allocate as it is needed. That is why list, in the end, use less total memory.

For sprite frames, I''d use a dynamic array. The stl vector would work good.


STL vectors, if I recall correctly, by default will reallocate by doubling their size when necessary. Thus, if you know the exact number of elements you will be using, you should pass that in when you''re defining the vector. I''m not sure of the exact technique for that, though.
Here''s the fastest way to get this done, and will also give the best performance.

Create the array with the new operator when you load the frames.

For each object structure that will use an animation, add ONE SINGLE pointer to a frame.

Set this pointer to the address of the first element in the array.

To get the next frame of animation, increment the pointer.

Do NOT access the frame through an index variable. Keep some index variables floating around to keep up with animation length, etc., but use the pointer to access the frames.

Accessing array elements with a pointer eliminates the offset calculation, if you don''t believe me try it both ways and check the assembly output.
quote:Original post by Anonymous Poster
Here''s the fastest way to get this done, and will also give the best performance.

Are you sure? I am not sure if you''re even really answering the question. You say ''the array'' without detailing how large to make it, which I believe was a key part of the original problem.
quote:Do NOT access the frame through an index variable. Keep some index variables floating around to keep up with animation length, etc., but use the pointer to access the frames.

Accessing array elements with a pointer eliminates the offset calculation, if you don''t believe me try it both ways and check the assembly output.

From what compiler? And for what platform? Unless I''m mistaken, recent processors have instructions that can process array offsets just as quickly as they would process any other indirect addressing (eg. dereferencing a pointer).

Anyway, even if pointer access is quicker than indexing, I doubt an offset calculation is going to be a big deal in relative terms when you''re just about to perform a blit anyway.

Write good code first, optimal code later...

This topic is closed to new replies.

Advertisement