Dynamic memory management

Posted by 40esp on Sun, 27 Feb 2022 05:38:39 +0100

The C library function void *malloc(size_t size) allocates the required memory space and returns a pointer to it.

statement
The following is the declaration of the malloc() function.

void *malloc(size_t size)
parameter
Size – the size of the memory block, in bytes.
Return value
This function returns a pointer to the allocated size of memory. NULL if the request fails.

example
The following example demonstrates the use of the malloc() function.

example

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main()
{
   char *str;
 
   /* Initial memory allocation */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %u\n", str, str);
 
   /* Reallocate memory */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);
 
   free(str);
 
   return(0);
}

Let's compile and run the above program, which will produce the following results:

String = runoob, Address = 3662685808 String = runoob.com, Address =
3662685808

C library function - realloc()
C standard library - < stdlib h> C standard library - < stdlib h>

describe

The C library function void *realloc(void *ptr, size_t size) attempts to readjust the size of the memory block before the malloc assigned by malloc or calloc.

statement
Here is the declaration of the realloc() function.

void *realloc(void *ptr, size_t size)
parameter
ptr – pointer to a memory block to be reallocated, which was previously allocated by calling malloc, calloc, or realloc. If it is a null pointer, a new memory block is allocated and the function returns a pointer to it.
Size – the new size of the memory block, in bytes. If the size is 0 and ptr points to an existing memory block, the memory block pointed to by ptr will be released and a null pointer will be returned.
Return value
This function returns a pointer to the reallocated size of memory. NULL if the request fails.

example
The following example demonstrates the use of the realloc() function.

example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
   char *str;
 
   /* Initial memory allocation */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %p\n", str, str);
 
   /* Reallocate memory */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %p\n", str, str);
 
   free(str);
   
   return(0);
}

Let's compile and run the above program, which will produce the following results:

String = runoob, Address = 0x7fa2f8c02b10 String = runoob.com,
Address = 0x7fa2f8c02b10

C library function - calloc()

C standard library - < stdlib h> C standard library - < stdlib h>

describe
The C library function void *calloc(size_t nitems, size_t size) allocates the required memory space and returns a pointer to it. The difference between malloc and calloc is that malloc does not set the memory to zero, while calloc sets the allocated memory to zero.

statement
The following is the declaration of the calloc() function.

void *calloc(size_t nitems, size_t size)
parameter
nitems – number of elements to be allocated.
Size – the size of the element.
Return value
This function returns a pointer to the allocated memory. NULL if the request fails.

example
The following example demonstrates the use of the calloc() function.

example

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
   int i, n;
   int *a;
 
   printf("Number of elements to enter:");
   scanf("%d",&n);
 
   a = (int*)calloc(n, sizeof(int));
   printf("input %d Number:\n",n);
   for( i=0 ; i < n ; i++ ) 
   {
      scanf("%d",&a[i]);
   }
 
   printf("The number entered is:");
   for( i=0 ; i < n ; i++ ) {
      printf("%d ",a[i]);
   }
   free (a);  // Free memory
   return(0);
}

Let's compile and run the above program, which will produce the following results:

Number of elements to input: 3 input 3 numbers: 22 55 14 input number: 22 55 14

The memory space allocated by calloc, malloc or realloc is called before the C library function void free(void *ptr) is released.

statement
Here is the declaration of the free() function.

void free(void *ptr)
parameter
ptr – the pointer points to a memory block to free memory, which was previously allocated by calling malloc, calloc, or realloc. If the parameter passed is a null pointer, no action will be performed.
Return value
This function does not return any value.

example
The following example demonstrates the use of the free() function.

example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
   char *str;
 
   /* Initial memory allocation */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %p\n", str, str);
 
   /* Reallocate memory */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %p\n", str, str);
 
   /* Free allocated memory */
   free(str);
 
   return(0);
}

Let's compile and run the above program, which will produce the following results:

String = runoob, Address = 0x7fe4e4c02b10 String = runoob.com,
Address = 0x7fe4e4c02b10

Topics: C