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?
-
Do you mean "contiguous with the original memory"?Barmar– Barmar2014-01-31 20:23:27 +00:00Commented Jan 31, 2014 at 20:23
2 Answers
Yes, just like
malloc()does.If you have an
int* ptrwhich is a pointer to an element of a dynamically allocatedintarray, a simpleptr++will point to the next element.Using
malloc()andrealloc()in C seems to me a good option for dynamic arrays.
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).