c++attributesdynamic-list

Memory efficient way to create a dynamic-list attribute that can grow indefinitely


I need to make an object which owns lists that can grow indefinitely. Items in the list are structs composed of basic types.

So, I want to know if using a vector for it could lead to memory fragmentation if it grows too much. If so, what should I use instead?

Would a pointer to vector be enough? I don't know if memory fragmentation would be less important if the vector is stored outside the object.


Solution

  • From the comments :

    n the biggest test case I have, the mother object with the biggest list has 10000 elements. However, there are 23000 mother objects in that case. So, we could speak of a total of 230,000,000 "basic structs" as maximum, given there are not bigger cases than this.

    Use vectors.

    You shouldn't worry about memory fragmentation when the biggest contiguous array of memory you need contains about 10000 elements (let's say 30 bytes per element, that means 300kB). Today's memory models are efficient enough, they can manage a few kilobytes of contiguous memory. If you want to know more about memory fragmentation, here's a question about it.

    The fact that you can have a lot of "mother objects" doesn't matter, since they don't require to be contiguous in memory.

    You can also read about deques if you want to dig a little deeper.