문제

  • 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?

도움이 되었습니까?

해결책 2

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).

다른 팁

  1. Yes, just like malloc() does.

  2. If you have an int* ptr which is a pointer to an element of a dynamically allocated int array, a simple ptr++ will point to the next element.

  3. Using malloc() and realloc() in C seems to me a good option for dynamic arrays.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top