Your browser doesn't support JavaScript Dynamic Memory Allocation - Windows Programming

Dynamic Memory Allocation

Allocating memory manually during run-time is known as dynamic memory allocation.  Dynamic memory allocation requires the programmer to know how much memory to allot when the program is written.  All the functions for handling dynamic memory allocation must include the header file stdlib.h.  

The amount of memory available for allocation will depend on the system.  When a memory allocation function is called, the return value must be checked to ensure the memory was allocated successfully. If the memory allocation fails the programs must be able to handle the failure.

There are 4 library functions defined for dynamic memory allocation in C. They are malloc(), calloc(), realloc() and free().

The malloc() Function

 The malloc() function allocates a memory block of the specified number of bytes. It returns a pointer of type void which can be cast into a pointer of any form. The prototype is –

#include <stdio.h>
#include <stdlib.h>
int main () {
 char *str;
/* Initial memory allocation */
 str = (char *) malloc(25);
 strcpy(str, "malloc memory string");
 printf("String = %s, Address = %u\n", str, str);
/* Reallocating memory */
 int *i;
 i = (int *) malloc(1 );
 *i=100;
 printf("Integer = %i, Address = %u\n", *i, i);
 free(str);
 free(i);
 return(0);
}

The calloc() function

calloc() allocates multiple blocks of memory and initialises them to zero.  The prototype is-

void *calloc(size_t nitems,size_t size)

nitems − This is the number of elements to be allocated.
size − This is the size of elements.

#include <stdio.h>
#include <stdlib.h>
int main () {
 int *ptr;
 ptr = (int*)calloc(4, sizeof(int));
 if (ptr != NULL)
puts("Memory allocation was successful.");
else
puts("Memory allocation failed.");
 for(int i=0 ; i < 4 ; i++ ) {
 ptr[i]=i;
 }
 for(int i=0 ; i < 4 ; i++ ) {
 printf("[%i]", ptr[i]);
 }
 free( ptr );
 return(0);
}

The realloc() Function 

If the dynamically allocated memory is insufficient or more than required, you can change the size of the block of memory previously allocated memory using realloc() function.  The prototype is-

ptr = realloc(void ptr,size_t size);

The ptr argument is a pointer to the original block of memory. The new size, in bytes, is specified by size.

The free() function

Dynamically allocated memory created with either calloc() or malloc() has to be manually released. The prototype is-

free(ptr);

The free() function releases the memory pointed to by ptr. This memory must have been allocated with malloc(), calloc(), or realloc().

#include <stdio.h>
#include <stdlib.h>
int main () {
 char *str;
 /* Initial memory allocation */
 str = (char *) malloc(20);
 strcpy(str, "malloc memory string");
 printf("\n String = %s", str );
 /* expand memory */
str = realloc(str, 60);
 strcpy(str, "expand malloc memory string to a total of 50 characters");
 printf("\n String = %s", str);
 str = realloc(str, 14);
 /*shrink memory */
 strcpy(str, "shrink memory ");
printf("\n String = %s", str);
 free(str);
 return(0);
}

Manipulating Memory Blocks

memset –  The memset() function sets all bytes in a block to a specified value. In addition, memset can also be used for copying and moving information from one location to
another.  The function prototype is

void *memset(void *dest, int c, size_t count);

The parameter dest points to the start of a block of memory to be changed, c is the value to set, and count is the number of bytes, starting at dest, to be set. 

memcpy – The memcpy() function copies a specified number of bytes of data between memory blocks. The function prototype is

void *memcpy(void *dest, void *src, size_t count);

The parameters dest and src point to the destination and source memory blocks and count specifies the number of bytes to be copied.  If the two blocks point to the same area of memory the function may fail

memmove – The memmove() function copies a specified number of bytes from one memory block to another however unlike memcpy() it can handle overlapping memory blocks. The prototype is

void *memmove(void *dest, void *src, size_t count);

The parameters dest and src point to the destination and source memory blocks. Count specifies the number of bytes to be copied. 

#include <stdio.h>
#include <string.h>
char messageorg[60] = "This is the original message";
char messagenew[60] = "";
int main( void )
{
printf("\n%s", messageorg);
memset(messageorg, 32, 7);
printf("\n%s", messageorg);
memmove(messagenew ,messageorg+12, 18);
printf("\n%s", messagenew);
return 0;
}