Filter by Tags

Define the working of the malloc function



0 0

Runtime memory allocation is done using the malloc function in C. Define the working of the malloc function. How can we use the memory allocated by the malloc function in our program.

Open E-School
Posted on : Wed, Jun 14, 2017
Views : 164

Ask Question

If you want to ask question you must LogIn or SignUp
Login SignUp

Answers (2)


Malloc allocate as much memory user want to use. It doesn't concern with how user will organize or use it. malloc provide the inital address of the allocated memory.

Structure of malloc function:

ptr = (type-casting*)malloc(byte size);

here ptr will point to the initial address, Type casting tells to which pointer will point to and at last byte size that needs to be allocated.


it is useful in program when programer don't know the size of something lets say array that user wanted. it will allocate memory that memory to array.

eg int *point = (int *)malloc(20);





Sahil Gaba
Wed, Jun 14, 2017

Edit Answer



The malloc function basically allocates memory dynamically from the heap. Suppose we do not know the exact size of a array to be used in the program, a number can be entered by the user and an array of that size can be allocated in the heap and can be accessed using pointers.

e.g. int *p=(int *)malloc(40);

The above staement allocates 40 bytes in the heap for the pointer p. The statement (int *) is basically type casting. It tells the malloc that the memory allocated is of pointer type. The pointer points to the base address of memory allocated by the memory. Thus 10 elements of int type can be stored in the memory allocated in the heap. This implies that it is an array of 10 elements.

The malloc function is only responsible for allocating memory and thus user organizes the memory in the way he wants.The user specifies the type for which he wants the memory to be allocated.   

The malloc function returns a void pointer.

Apurva Thakker
Wed, Jun 14, 2017

Edit Answer

This answer will permanently deleted. Do you really want to delete this Answer ?
To Add your Answer Please
LogIn | SignUp

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp