c - Reallocating an array (C99) -


The content of the reclaimed space is undefined if the new size is larger.

If the contents of a protected pre-allocated space are important, this is the best way to reassign the data: copy it to a stack, freeze it, place it on a pile with more space Doing, and copying over the heap? Is there another safe way to do this?

Is the best way to implement a data structure like a dynamic array increasingly in a linked list?

"The content of the new allocated part of the item is unspecified." Your content will still be in the beginning of the returned memory area.

Tell me:

  char * p = malloc (6); If (p == NULL) {...} memcpy (p, "hello", 6); Char * temp = realloc (P, 12); If (temp == NULL) {...} p = temp; The first 6 letters are guaranteed on  

p 'H', 'E', 'L', 'L', 'O', '0 ', Even if the new p is same as the old p The remaining 6 "new" characters are all undefined.


Comments