Memory Management In C Language

aimtocode

Memory management

Memory management functions handle the allocation and deallocation of dynamic memory. These functions form an abstraction layer above the standard C memory management functions malloc, free, and realloc.

This block of functions can be replaced by the user with custom code to implement a different memory management scheme. For example, an embedded system application might want to use a fixed-sized static block from which to allocate.

The built-in memory management logic implements a nibble-allocation memory management algorithm that provides superior performance to calling malloc and free directly.

This algorithm causes memory blocks to be allocated up front in larger sizes and then subsequently split up when future allocation requests are received.

These blocks can be reset and reused in applications that are constantly allocating and freeing memory for example, a decoder that constantly reads and decodes XML messages in a long running loop.

Static allocation

Static allocation is what happens when you declare a static or global variable. Each static or global variable defines one block of space, of a fixed size. The space is allocated once, when your program is started (part of the exec operation), and is never freed.

Automatic allocation

Automatic allocation happens when you declare an automatic variable, such as a function argument or a local variable. The space for an automatic variable is allocated when the compound statement containing the declaration is entered, and is freed when that compound statement is exited.

Dynamic Memory Allocation

Dynamic memory allocation is a technique in which programs determine as they are running where to store some information. You need dynamic allocation when the amount of memory you need, or how long you continue to need it, depends on factors that are not known before the program runs.

malloc() function in C

  • The malloc() function allocates single block of requested memory.
  • It doesn't initialize memory at execution time, so it has garbage value initially.

Syntax:

  
  ptr=(cast-type*)malloc(byte-size)


Example:

 #include<stdio.h>
 #include<stdlib.h>
 int main()
 {
    int i,length,sum=0;
    printf(" Enter the count of numbers:");
    scanf("%d", &length);
    int *p;
    p=(int*) malloc((sizeof(int)*length);
    for(i=0; i<length;i++)
    {
        printf("Enter a number :");
        scanf("%d",p+i);
    }
    for(i=0; i<length;i++)
    {
        sum=sum+*(p+i);
    }
    return 0;
 }

After using malloc() function memory must be restored using de-allocation. The memory has to be de-allocated to efficiently use the memory space. So, to free up the memory allocated, use free() function. Then that freed space is ready to use again

Example:

 #include<stdio.h>
 #include<stdlib.h>
 int main()
 {
    int i,length,sum=0;
    printf(" Enter the count of numbers:");
    scanf("%d", &length);
    int *p;
    p=(int*) malloc((sizeof(int)*length);
    for(i=0; i<length;i++)
    {
        printf("Enter a number :");
        scanf("%d",p+i);
    }
    for(i=0; i<length;i++)
    {
        sum=sum+*(p+i);
    }
    return 0;
 }

calloc() function in C

  • The calloc() function allocates multiple block of requested memory.
  • It initially initialize all bytes to zero.
  • It returns NULL if memory is not sufficient.

Syntax:


 ptr=(cast-type*)calloc(number, byte-size)  


Example:

 #include<stdio.h>
 #include<string.h>
 #include<stdlib.h>
 int main()
 {
    int length,i;
    int *ptr;
    printf("Enter count of number:");
    scanf("%d",&length);
    ptr=(int*)calloc(length,sizeof(int));
    if(ptr==NULL)
    {
        printf("Cannot allocate memory");
        exit(1);
    }
    for(i=0;i<length;i++)
    {
        printf("Enter a number %d: ",i);
        scanf("%d",&ptr[i]);
    }
    for(i=0;i<length;i++)
    {
        printf("Enter a number %d: ",i);
        scanf("%d",ptr[i]);
    }
    return 0;
 }
aimtocode