Reading:  

Getting started with C Language


Dynamic memory allocation in C

Dynamic memory allocation

As we know for each and every variable used we must allocate memory for that. We can allocate memory at compile time as well run time. The allocation of memory while compile time is called static memory allocation. The memory allocation during the execution of a program is called dynamic memory allocation. In many situations it is not possible to know the size of memory needed until run time. Using dynamic memory allocation at execution time of the program we can request more memory from the free memory pool ( heap). The only way to access memory at execution time is through pointers.  

Library functions for dynamic memory allocation

There are many built-in standard library functions for efficient dynamic memory allocation and de-allocation.  Their prototypes are declared in alloc.h and stdlib.h. The functions are

Calloc (), malloc () , free () and realloc ()

malloc ()

The function malloc () allocates a block of ‘size’ bytes from the free data area (heap). It allows a program to allocates an exact amount of memory explicitly, as when it is needed.

Syntax :

void  * malloc (size_t size);

the parameter passed to malloc is of the type size_t. This type is declared in the header file stddef.h. size_t is equivalent to the unsigned int data type. Thus in compilers where an int is 16 bits in size, malloc can allocate a maximum of 64 KB at a time.

Heap

Heap is used foe dynamic allocation of variable sized memory. Many data structures such as trees, and   lists use memory allocation from the heap.

Return value

malloc () returns a pointer to the newly allocated block of memory. If enough memory does not exist for new block malloc () returns NULL. The constant NULL is defined in the header file stdio.h to have a value zero. It is not necessary to use the constant NULL. We can substitute a value 0 to NULL.

Example :

int * buf;
buf = ( int * ) malloc ( 200 * sizeof(int) );
if (buf ==0)         
   printf(“Not enough memory……!!!” );

here we use a ‘0’ instead of NULL. The same we can write as..

int * buf;
buf = ( int * ) malloc ( 200 * sizeof(int) );
if (buf ==NULL)
printf(“Not enough memory……!!!” );

free

free () de-allocates a memory block allocated previously by calloc() or realloc(). Many DOS compilers offer the functions farmalloc() , farfree() etc . That can be used to obtain far pointers in the small and medium models.

Syntax  :

void free ( void * block);

calloc()

calloc () provides access to the C memory heap, which is available for dynamic allocation of variable sized block of memory.

Syntax :

void *calloc (size_t nitems , size_t size);

 Unlike malloc (), the function calloc () accepts two arguments nitems, and size. The parameter nitems specifies the number of items to allocate and size specifies the size of each item. For example , to allocate 10 integers we can write

ary = (int *) calloc(n, sizeof(int) );

The function calloc() allocates a block of size (nitems*size) . one important difference between malloc() and calloc() is that calloc() initialize all bytes in the allocated block to zero.

Return value

On success ( if the memory is available) calloc () returns a pointer to newly allocated block. On failure ( if the memory is not available for the new block)  calloc () returns NULL. These functions returns void pointers. They should be typecast to the appropriate data type before using them to access the memory allocated.

Realloc

realloc () adjusts the amount of memory allocated to the block to size, copying the contents to a new location if necessary.

Syntax :

void *realloc(void *block, size_t size);

Where block argument points to a memory block previously obtained by calling malloc(), calloc() , realloc(). If block is a null pointer realloc() works just like malloc(). And size is the new size for allocated block. The function realloc () is useful when the maximum size of allocated block cannot be decided in advance.

Return values

On success the function returns the address of the reallocated block, which might be different from the address of the original block. On failure the function returns a NULL.

Self referential structures

A self referential structure is one which contains a pointer to its own type. This is explained using the example below

struct student
{
int rolno;
float marks;
int subject;
struct student *next;
};     

The next is a pointer to a structure variable of the type student.

The figure represents a linked list. In linked lists each node is added dynamically when it needs a new node. Each node have two parts one information part another link part. The info part stores the data and link part consists of the pointer to the next node. The last node has a NULL value in its link part.

 

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!