realloc in c

Solutions on MaxInterview for realloc in c by the best coders in the world

showing results for - "realloc in c"
Andy
28 Sep 2016
1/*prototype of realloc*/
2void *realloc(void *ptr, size_t newsize);
3
4/*use*/
5dyn_array = realloc(dyn_array, (dyn_array_size + 1) * sizeof(int));
6
7/*
8	adds one more space to dyn_array
9    you need stdlib.h for this
10*/
Elena
29 Mar 2020
1int ptr_new = *realloc(void *ptr, size_t size); 
2Where 'ptr_new' is the new pointer you want to point to the required Dynamic
3memory, 'ptr' is the already existing pointer to a dynamic memory and 'size'
4is the new size of dynamic memory that you want to allocate.
5
6Working:
7Realloc creates/allocates a new memory in heap with specified size and copies
8all the contents from the previous memory pointer to by the pointer 'ptr'.
9It the deallocates the memory in heap pointed to by the old pointer i.e, 'ptr'.
10Note: pointer 'ptr' should also point to a memory in heap.
Fynn
25 Aug 2017
1void *realloc(void *ptr, size_t size)
Niclas
21 Mar 2018
1#include <stdio.h>
2int main () {
3   char *ptr;
4   ptr = (char *) malloc(10);
5   strcpy(ptr, "Programming");
6   printf(" %s,  Address = %u\n", ptr, ptr);
7
8   ptr = (char *) realloc(ptr, 20); //ptr is reallocated with new size
9   strcat(ptr, " In 'C'");
10   printf(" %s,  Address = %u\n", ptr, ptr);
11   free(ptr);
12   return 0;
13}