Search code examples
carraysdynamicstructuredynamic-arrays

Dynamic, unpreditable sized array in C


I am going to make an array of structure values. The number of the entries depend on the input so there is no way to estimate the length of the array.

In a FOR loop, parsing the input, I would create an entry for every iteration. That means I need to reallocate array because the size grows and this leads to inefficiency in terms of performance.

If I were allowed to program in C++, I would use vector. Unfortunately I can't, and I can't think of any better idea.

Please help me, any advice would be appreciated.


Solution

  • If I were allowed to program in C++, I would use vector.

    Then you can do what vector would do: when you need to reallocate, double the size of the allocation. That should amortize realloc performance issues.