Dynamic memory management
Dynamic memory management is basically used to calculate runtime memory requirements. With this, we can not only save the amount of effort needed while developing the program but also saves us from recruiting the entire program. Basically there two major functions namely malloc() and calloc() referred to be dynamic memory management functions.
malloc()
it reserves a contiguous block of memory whose size in bytes is specified at “memory size”. When a program obtains a memory block through malloc(), its contents remain undermined. the malloc() takes one argument. And allocates garbage value initially.
Syntax: void *malloc(size_t memory_size);
Example:
int *arr, number=5; arr=(int*)malloc(number * sizeof(int)); /* - it creates 5 integer variables memory dynamically and returns its base address into 'arr' - malloc function doesnt creates all 5 variables memoery in single step */
calloc():
It reserves a block of memory whose size in bytes is at least count x size. In other words, the block is large enough to hold an array of count elements, each of which takes up a size in bytes. calloc() takes two arguments and allocates zeroes initially.
Syntax: void *calloc(size_t count, size_t size);
int *arr, number=5; arr=(int*)calloc(number, sizeof(int)); /* - it creates 5 integer variables memory dynamically and returns its base address into 'arr' - calloc function creates all 5 variables memoery in single spet */
realloc():
causes resizing of the allocated memory block. The below example goes to a new memory address and the first place occupied with old data and returns memory address to ‘arr’. (here 100 is required extra memory).
Syntax: void *realloc(void*, size_t size);
int *arr, number=5; arr=(int*)calloc(number, sizeof(int)); ....... ....... arr = (int*)realloc(10 * sizeof(int))); /* - realloc creates 10 integer variables memory block dynamically - after allocation it will copy old 5 variables into newly allocated block automatically - after coping old data it will delete */
free():
causes releases of the already allocated memory block.
int *arr; arr = (int*)malloc(5*sizeof(int)); ..... ..... free(arr);
[quote]above all four functions available in stdlib.h header file.[/quote]
Command line arguments
Arguments passed to a program from the command line are known as command line arguments. Programs written in ‘C’ language can also be invoked with the command line arguments. In this case, the operating system provides information about the arguments to the main() function.
On the other hand, the main() function has to be declared with two parameters i.e., argc and argv[]. argv[] stands for a number of arguments.
The default value of argc=1 in C and C++. Whereas it is 0 in java. Using command line arguments it is possible to create your own commands and can run in a DOS operating system.
#include <stdio.h> int main(int argc, char *argv[]) { int i; printf("\n number of arguments = %d \n", argc); printf("the arguments passed are"); printf("\n"); for (i=0;i<=argc-1;i++) printf(“%s\n”,argv[i]); return 0; }