Search code examples
carrayspointersdynamicrealloc

Dynamic Array Implementation in C


  • Does realloc function in C allocate contiguous memory space?

  • I am trying to implement dynamic integer array. Should I increment pointer by size of array element or by 1?

  • Are there any other better ways to implement dynamic array in C?


Solution

  • Does realloc function in C allocate contiguous memory space?

    Yes.

    I am trying to implement dynamic integer array. Should I increment pointer by size of array element or by 1?

    You should increase your pointer by 1.

    Are there any other better ways to implement dynamic array in C?

    Using malloc family functions is the only way. But in C99 and latter you can use variable length arrays (but it has some limitations as it allocates memory on stack).