So, i want to create a wall system, i need to be able to add segments to an array, but i don't want to pre allocate crazy memory, how do i work with this?

I create a new array which is one element bigger, copy everything from the older one, and put the new data in the empty slot, and then remove the old one from memory?

@YoSoyFreeman The way that it's commonly done (and how most List<T> type structures work under-the-hood) is that it starts out with a reasonable default capacity (say, 16) and tracks its count separately from its capacity. Then, whenever it needs to expand (count > capacity), it doubles capacity, and copies everything over to the new doubled space.

That minimizes the amount of churn while still allowing for expansion.

@LouisIngenthron Thank you! Thank makes a lot of sense. However, then i have a new array, do i just update the pointer to point to this new one instead?

@YoSoyFreeman Once you've copied over the data and freed the old memory, yes.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.