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

Questions by satyajit

Showing Answers 1 - 13 of 13 Answers

Raagu

  • Dec 8th, 2006
 

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

  Was this answer useful?  Yes

Guest

  • 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

  Was this answer useful?  Yes

fcawad_03

  • 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.

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.

  Was this answer useful?  Yes

Gautam Bhalla

  • 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.

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions