GeekInterview.com
Series: Subject: Topic:
Question: 149 of 238

what is the meaning memory allocation & why we use it?

Asked by: satyajit | Member Since Jan-2006 | Asked on: Feb 17th, 2006

View all questions by satyajit

Showing Answers 1 - 5 of 5 Answers
Raagu

Answered On : Dec 8th, 2006

Memory allocation refers to reserving memory for a variable storage. The variable can be an array or a datastructure.

  
Login to rate this answer.
Guest

Answered On : Feb 16th, 2007

Memory Allocation::According to the conceptual view the program instructions and global and static variable in a permanent storage area and local area variables are stored in stacks. The memory space that is located between these two regions in available for dynamic allocation during the execution of the program. The free memory region is called the heap. The size of heap keeps changing when program is executed due to creation and death of variables that are local for functions and blocks. Therefore it is possible to encounter memory overflow during dynamic allocation process. In such situations, the memory allocation functions mentioned above will return a null pointer. ......Avay

  
Login to rate this answer.
fcawad_03

Answered On : Feb 16th, 2007

View all answers by fcawad_03

Memory Allocation:::According to the conceptual view the program instructions and global and static variable in a permanent storage area and local area variables are stored in stacks. The memory space that is located between these two regions in available for dynamic allocation during the execution of the program.

The free memory region is called the heap. The size of heap keeps changing when program is executed due to creation and death of variables that are local for functions and blocks. Therefore it is possible to encounter memory overflow during dynamic allocation process. In such situations, the memory allocation functions mentioned above will return a null pointer.

Allocating a block of memory: A block mf memory may be allocated using the function malloc. The malloc function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer.

It takes the following form: ptr=(cast-type*)malloc(byte-size); ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an area of memory with size byte-size. Example: x=(int*)malloc(100*sizeof(int));

On successful execution of this statement a memory equivalent to 100 times the area of int bytes is reserved and the address of the first byte of memory allocated is assigned to the pointer x of type int Allocating multiple blocks of memory: Calloc is another memory allocation function that is normally used to request multiple blocks of storage each of the same size and then sets all bytes to zero.

The general form of calloc is: ptr=(cast-type*) calloc(n,elem-size); The above statement allocates contiguous space for n blocks each size of elements size bytes. All bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not enough space a null pointer is returned.

Releasing the used space: Compile time storage of a variable is allocated and released by the system in accordance with its storage class. With the dynamic runtime allocation, it is our responsibility to release the space when it is not required.

The release of storage space becomes important when the storage is limited. When we no longer need the data we stored in a block of memory and we do not intend to use that block for storing any other information, we may release that block of memory for future use, using the free function. free(ptr); ptr is a pointer that has been created by using malloc or calloc.

  
Login to rate this answer.
Gautam Bhalla

Answered On : Jul 20th, 2007

Memory Allocation:--Memory allocation is nothing but allocation of a block of memory or storage register from RAM to keep track of previous instractions or variables we are dealing with.

Need:-Memory allocarion is important to keep the track of current value of the data items and instructions.

  
Login to rate this answer.

allocation of memory for a particular variable like is done because to save unusage of memory

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.