Realloc() Method

Realloc() method stands for reallocation of memory. Sometimes malloc() or calloc() method allocate memory need to be dynamically change that is happened by realloc() method. If, the malloc() or calloc() allocate memory is insufficient then re-allocation is occurred by realloc() method.

Syntax

  1. ptr = realloc(ptr, reallocate_size);

Example

malloc memory allocated size:

  1. ptr = (int*) malloc(400 * sizeof(int));

newly reallocate size:

  1. ptr = realloc(ptr, 500 * sizeof(int));

Here is a program example of dynamic memory allocation using realloc() method.

Example:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main() {
  4. int *ptr
  5. int a, b;
  6. a = 10;
  7. printf("Enter number : %d\n", a);
  8. // Dynamically allocate memory using malloc()
  9. ptr = (int*)malloc(a * sizeof(int));
  10. if (ptr == NULL) {
  11. printf ("Memory is not allocated.\n");
  12. exit(0);
  13. }
  14. else {
  15. printf ("Successfully allocated memory using malloc.\n");
  16. for(b = 0; b < a; ++b) {
  17. ptr[b] = b + 1;
  18. }
  19. printf("The elements of the array are: ");
  20. for(b = 0; b < a; ++b) {
  21. printf ("%d", ptr[b]);
  22. }
  23. }
  24. // reallocation_of memory
  25. a = 20;
  26. ptr = realloc(ptr, 20 * sizeof(int));
  27. if (ptr == NULL) {
  28. printf ("Memory is not allocated.\n");
  29. exit(0);
  30. }
  31. else {
  32. printf ("Successfully allocated memory using malloc.\n");
  33. for(b = 0; b < a; ++b) {
  34. ptr[b] = b + 1;
  35. }
  36. printf("The elements of the array are: ");
  37. for(b = 0; b < a; ++b) {
  38. printf ("%d", ptr[b]);
  39. }
  40. }
  41. return 0;
  42. }


Free() Method

calloc() and malloc() mehod cannot automatically release the memory . So, free() method used with calloc() or malloc() method release the dynamically allocated memory. The method free() stands for de-allocation.

Syntax

  1. free(ptr);

Here is a program example(calloc memory release) using free() method.

Example(calloc memory release):

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main() {
  4. int *ptr
  5. int a, b;
  6. a = 10;
  7. printf("Enter number : %d\n", a);
  8. // Dynamically allocate memory using calloc()
  9. ptr = (int*)calloc(a, sizeof(int));
  10. if (ptr == NULL) {
  11. printf ("Memory is not allocated.\n");
  12. exit(0);
  13. }
  14. else {
  15. printf ("Successfully allocated memory using calloc.\n");
  16. for(b = 0; b < a; ++b) {
  17. ptr[b] = b + 1;
  18. }
  19. printf("The elements of the array are: ");
  20. for(b = 0; b < a; ++b) {
  21. printf ("%d", ptr[b]);
  22. }
  23. }
  24. //memory release for calloc()
  25. free(ptr);
  26. return 0;
  27. }

Output:

Enter number : 10
Successfully allocated memory using calloc.
The elements of the array are: 1 2 3 4 5 6 7 8 9 10

Here is another program example(malloc memory release) using free() method.

Example(malloc memory release):

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main() {
  4. int *ptr
  5. int a, b;
  6. a = 10;
  7. printf("Enter number : %d\n", a);
  8. // Dynamically allocate memory using malloc()
  9. ptr = (int*)malloc(a, sizeof(int));
  10. if (ptr == NULL) {
  11. printf ("Memory is not allocated.\n");
  12. exit(0);
  13. }
  14. else {
  15. printf ("Successfully allocated memory using malloc.\n");
  16. for(b = 0; b < a; ++b) {
  17. ptr[b] = b + 1;
  18. }
  19. printf("The elements of the array are: ");
  20. for(b = 0; b < a; ++b) {
  21. printf ("%d", ptr[b]);
  22. }
  23. }
  24. //memory release for malloc()
  25. free(ptr);
  26. return 0;
  27. }

Output:

Enter number : 10
Successfully allocated memory using malloc.
The elements of the array are: 1 2 3 4 5 6 7 8 9 10