[go: up one dir, main page]

0% found this document useful (0 votes)
35 views3 pages

Dynamic Memory Allocation in C

Dynamic memory allocation in C allows for memory to be allocated during execution using functions like malloc(), calloc(), realloc(), and free(), which manage memory in the heap. The malloc() function allocates a single block of memory, calloc() allocates multiple blocks and initializes them to zero, realloc() adjusts the size of previously allocated memory, and free() deallocates memory when it's no longer needed. Proper use of these functions is essential to avoid memory leaks and ensure efficient memory management.

Uploaded by

hp1509032014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

Dynamic Memory Allocation in C

Dynamic memory allocation in C allows for memory to be allocated during execution using functions like malloc(), calloc(), realloc(), and free(), which manage memory in the heap. The malloc() function allocates a single block of memory, calloc() allocates multiple blocks and initializes them to zero, realloc() adjusts the size of previously allocated memory, and free() deallocates memory when it's no longer needed. Proper use of these functions is essential to avoid memory leaks and ensure efficient memory management.

Uploaded by

hp1509032014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Dynamic Memory Allocation in C

 The process of allocating memory at the time of execution is called dynamic memory
allocation.
 The allocation and release of this memory space can be done with the help of some
built-in functions whose prototype are found in stdlib.h header file
 These functions allocate memory from a memory area called heap and release this
memory whenever not required.
 malloc()
 calloc()
 realloc()
 free()

malloc() Function
The C malloc() function stands for memory allocation. It is a function which is used to allocate a
block of memory dynamically. It reserves memory space of specified size and returns the null
pointer pointing to the memory location. The pointer returned is usually of type void. It means
that we can assign C malloc() function to any pointer.

Syntax of malloc() Function:

ptr = (datatype *) malloc (specified size);

Here,

 ptr is a pointer of cast_type.


 The C malloc() function returns a pointer to the allocated memory of byte_size.

Example of malloc():

Example: ptr = (int *) malloc (50)

When this statement is successfully executed, a memory space of 50 bytes is reserved. The
address of the first byte of reserved space is assigned to the pointer ptr of type int.

ptr = (int *) malloc (50*sizeof(int))

1
calloc() Function
The C calloc() function stands for contiguous allocation. This function is used to allocate multiple
blocks of memory. It is a dynamic memory allocation function which is used to allocate the
memory to complex data structures such as arrays and structures.

Malloc() function is used to allocate a single block of memory space while the calloc() in C is
used to allocate multiple blocks of memory space. Each block allocated by the calloc() function is
of the same size.

Syntax of calloc() Function:

ptr = (cast_type *) calloc (n, size);

 The above statement is used to allocate n memory blocks of the same size.
 After the memory space is allocated, then all the bytes are initialized to zero.
 The pointer which is currently at the first byte of the allocated memory space is returned.

Whenever there is an error allocating memory space such as the shortage of memory, then a null
pointer is returned.

2
realloc() Function
Using the C realloc() function, you can add more memory size to already allocated memory. It
expands the current block while leaving the original content as it is. realloc() in C stands for
reallocation of memory.

realloc() can also be used to reduce the size of the previously allocated memory.

Syntax of realloc() Function:

ptr = realloc (ptr,newsize);

The above statement allocates a new memory space with a specified size in the variable newsize.
After executing the function, the pointer will be returned to the first byte of the memory block.
The new size can be larger or smaller than the previous memory. We cannot be sure that if the
newly allocated block will point to the same location as that of the previous memory block. This
function will copy all the previous data in the new region. It makes sure that data will remain
safe.

The free() Function


The memory for variables is automatically deallocated at compile time. In dynamic memory
allocation, you have to deallocate memory explicitly. If not done, you may encounter out of
memory error.

The free() function is called to release/deallocate memory in C. By freeing memory in your


program, you make more available for use later.

free(ptr);

You might also like