C'Language - [DMA Functions]

♠ Posted by Unknown in at 03:08

Dynamic Memory Allocation Function:

When we declare an array in out program, the compiler allocates memory to hold the array prior to providing it. Some times it is required to change the size of the array and in that case it is required to edit and compile the program. To reduce the number of changes, it is required to allocate required memory during the run-time. For example, suppose we want to read a file into memory from the disk. If the size of file is not known and we have to load it into memory and defining very large array, so that file can fit into the array. If the size of the array is larger than that of file then the space is wasted. If the size of file is larger then it is not possible to load the complete file into the array, leading to need for dynamic memory allocation. In the dynamic memory allocation we use a pointer variable and it’s size is allocated during the execution of the program. 

There are two principle functions in C which are used to allocate required memory to pointer variable.
                                   
1.      malloc()
2.      calloc()

Both the malloc() and calloc() functions performs the operations and checks memory availability, based on the memory block size requested. When a suitable segment of memory is located. A pointer to the address of the allocated block is returned by the function.

1. malloc()

This function is used to allocate a requested number of bytes from the free pool of memory, and returns a pointer to a block of contiguous memory of the size specified.

This function allocates a block of memory and returns a pointer which points the first byte of the allocated space or memory block and if memory allocation was not successful, it returns a NULL pointer.

Syntax :

<pointer> = (<cast type> *) malloc(<no. of bytes to be allocated>);

“Pointer” is the name of a pointer of type cast type.
The malloc() functions returns a pointer of cast type to an area of memory with size as no. of bytes to be allocated.

Example:                                   
                                    mpointer = (int *) malloc(50 * sizeof(int));

After the successful execution of the above statement, a memory space equivalent to “50 times the size of an int” is reserved, and the address of the first byte of this block of memory allocated is assigned to the pointer mpointer of type int.

If an integer takes  2 bytes of storage space, the above declaration will allocate 100 bytes of contiguous storage space.

The malloc() function always returns a pointer to the first byte of the block of memory that has been allocated. It allocates a contiguous block of memory. If the allocation fails, i.e. malloc() is not able to allocate the required bytes of memory as a contiguous block, it returns NULL pointer.

2. calloc()

‘C’ also provides the calloc() function for allocating memory.
This function is normally used to request memory allocation at run time for storing derived data types such as arrays and structures.

calloc() function allocates multiple blocks of storages, each block of the same size and then sets all the bytes of memory in the block to zero.
Syntax:

<pointer> = (<cast-type> *) calloc(<no. of blocks>, <size of element>);

The calloc() function allocates contiguous space of “no. of block”, each block being of size, “size of element”. All the bytes of memory allocated are then initialized to zero, and a pointer to the first byte of the allocated contiguous memory is returned. In case, there is not enough memory available in the  free pool of memory, a NULL pointer is returned.

3. free()

When we are dynamically allocating memory for variables at run time, the memory is not automatically released to the system. It is responsibility of a programmer, who has dynamically allocated memory, to released the memory to the free pool of memory when the memory is not needed.

‘C’ provides the free() function to release a block of memory that was allocated and is no longer needed.
Syntax:
                                    free(<pointer>);

“pointer” is a pointer to the memory block that was returned by either malloc() and calloc() function calls.

4. realloc()

After, memory has been allocated to variables at run time, there may be a need to change the size of block of memory that was allocated. ‘C’ provides the realloc() function, using which the memory can be reallocated to increase or decrease the size of allocated memory.

Syntax:
                                    <pointer> = realloc(<pointer>, <new memory size>);

“pointer” is the pointer that points the first byte of the memory that was allocated using either malloc() or calloc() functions. “new memory size” indicates the new size of memory that is to be allocated. This new size could be more or less than the original size.

It is important to note here that the new block of memory may or may not begin at the same place as the old block.

In case, the functions fails to reallocate a larger memory in contiguous block at the location, it will create the same as an entirely new region and move the contents of the old block into the new block. The function guarantees that the old data will remain intact.

If the realloc() function fails to allocates additional space, it returns the NULL pointer and the original block is lost. Therefore, it is necessary for a programmer to test for the success of the function before reallocating memory, so that, old data is not lost.

0 comments:

Post a Comment