How can you increase the size of a dynamically allocated array? |
[Edit] |
int *ptr = malloc(10 * sizeof (int));
if (ptr == NULL) { /* Memory could not be allocated, the program should handle the error here as appropriate. */
realloc
It is often useful to be able to grow or shrink a block of memory. This can be done using realloc which returns a pointer to a memory region of the specified size, which contains the same data as the old region pointed to by ptr (truncated to the minimum of the old and new sizes). If realloc is unable to resize the memory region in-place, it allocates new storage, copies the required data, and frees the old pointer. If this allocation fails, realloc maintains the original pointer unaltered, and returns the null pointer value. The newly allocated region of memory is uninitialized (its contents are not predictable). The function prototype is
void *realloc(void *pointer, size_t size);
First answer by ID3411788082. Last edit by Neha 0782. Contributor trust: 8 [recommend contributor]. Question popularity: 17 [recommend question]
|
Research your answer: |



