What the use of malloc() function?

What the use of malloc() function?

malloc() function is used for memory allocation in ANSI C standard.malloc() function requires only one parameters. 
For using malloc() function we have to follow some rules. Which are given below:

1.The malloc() function returns a pointer to the allocated memory or NULL if the
memory could not be allocated.
2.The pointer returned by malloc() should be saved in a static variable, unless you are sure that the memory block will be freed before the pointer variable is discarded at the end of the block or the function.
3.You should always free a block of memory that has been allocated by malloc()when you are finished with it. If you rely on the operating system to free the block when your program ends, there may be insufficient memory to satisfy
additional requests for memory allocation during the rest of the program�s run.
4.Avoid allocating small blocks (that is, less than 25 or 50 bytes) of memory.There is always some overhead when malloc()allocates memory�16 or more bytes are allocated in addition to the requested memory.

By:Vivek Kr. Agarwal
Date:2007-01-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related C Subjective Links

C Subjective interview questions and answers for experienced and fresher