Dynamic memory management : Dynamic memory management is a technique to managing the memory of the computer dynamically. Dynamic memory management technique permit us to allocate additional memory space or remove unnecessary memory space when program execution is done (called runtime programming). So when it removes unnecessary memory space as a result size increases. Alternatively , we can say dynamic memory management optimizes the storage space of computer. There are four memory allocation functions are used in dynamic memory allocation. These are (1) malloc() function , (2) calloc() function, (3) realloc() function , (4) free() function.

The c programming language consists of set of local variable, global variable and program instruction. Among the three generally local variables are stored in stack region. Program instruction and global variable are stored in computers permanent storage region. Heap region is the dynamic memory allocation. The size of the heap changing when program is executed due to creation and deleted .



(1). malloc() Function : This is a function which allocates a single block of memory dynamically.

The general form is ,

p=(data_type*) malloc(size specified);



Here size specified means the number of bytes to be allocated, p is a pointer of type data-type, malloc returns a pointer of data type to an area of memory with specified size.

Example:

int *p;

p=(int *)malloc(20);

the function creates 20 bytes of memory whose base address is stored in pointer variable p. Since p is integer it creates 10 memory location whose size is 2 bytes.

Another form of declaration is,

p=(data_type*)malloc(number of space*sizeof(data_type));

Example:

int *p;

p=(int *)malloc(10*sizeof(int));

Here it creates memory locations to store integer values.

The malloc function allocates a continuous block of bytes . If the heap memory is not sufficient then the allocation process fails. If fails, it returns a NULL.



(2). calloc() Function : This function is the memory allocation function which is used to allocate multiple blocks of memory each of same size and then sets all bytes to zero. This function is used for allocating memory space dynamically for storing derived data type such as structure and arrays.

The general form is ,

p= (data_type*) calloc(n, sizeof(data_type));

This declaration allocates contagious n memory block/space each of size of respective data type.

Example:

int *p;

p=(int*)calloc(5,sizeof(int));

This declaration allocates 5 memory block of size 2 bytes and starting address/base address is stored in pointer variable p which is int type.



(3). realloc() Function : This function is another memory allocation function which is used to modify the existing memory size without losing the previous data. If the memory allocated is larger than the required memory then we want to reduce it. If previous allocated memory is not sufficient then we can increase. This process is called reallocation of memory.

The general form is,

p=(int *)realloc(p,modified_size);

In this declaration two arguments are there, first one p is pointer to the previously allocated block of memory by the function malloc() or calloc(). The second one is the modified_size which allocates new memory location to the pointer variable p. p argument stores the base (starting) address of memory. If modified_size is larger, then old data is not lost.



(4). free() Function : In dynamic memory allocation, the programmer's responsibility is to remove the memory when it is not required. When we do not want to the data to store the data in a block of memory then we intend to remove that block of memory which we will not use in future. Then we use the function free.

The general form/syntax is,

free(p);

p is a pointer to a block of memory which has been created by malloc or calloc function.





Command-line arguments : Like user-defined function takes arguments, the main function also takes the arguments by default. But we do not mention the arguments in writing the program. The main is also a user defined function which takes two arguments called argument counter abbreviated as argc and other is argument vector abbreviated as argv.

The variable argument counter (argc) count the number of arguments on the command line. The argv represents an array of character pointer that points to the command line arguments. The argc is the size of the array.

In order to access the command line arguments, we must declare the main function and its parameters are,

void main(int argc, char *argv[]);

{

}



In the above declaration argv[0] hold the name of the program and argv[1] is pointer to the first command line argument supplied and *argv[n] is the last argument. If no arguments are supplied , argc will be one , otherwise and if you pass one argument then argc is set at 2.





THANKS

