0% found this document useful (0 votes)
97 views11 pages

System Design and Analysis - Dynamic Memory Allocation

The document discusses dynamic memory allocation functions in C: 1) malloc() reserves a block of memory of a specified size and returns a pointer to it, leaving the memory uninitialized. 2) calloc() allocates contiguous blocks of memory for an array, initializing the memory to zero. 3) free() deallocates dynamically allocated memory to avoid memory leaks. 4) realloc() changes the size of a previously allocated memory block.

Uploaded by

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

System Design and Analysis - Dynamic Memory Allocation

The document discusses dynamic memory allocation functions in C: 1) malloc() reserves a block of memory of a specified size and returns a pointer to it, leaving the memory uninitialized. 2) calloc() allocates contiguous blocks of memory for an array, initializing the memory to zero. 3) free() deallocates dynamically allocated memory to avoid memory leaks. 4) realloc() changes the size of a previously allocated memory block.

Uploaded by

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

Dynamic Memory Allocation in C

Dr. Sheak Rashed Haider Noori


Associate Professor & Associate Head
Dynamic Memory Allocation in C

In C language there are 4 library functions under


"stdlib.h" for dynamic memory allocation.
malloc()

The name malloc stands for "memory allocation”. The function


malloc() reserves a block of memory of specified size and
return a pointer of type void which can be casted into pointer
of any form.
Prototype:
void* malloc(size in byte);

Syntax:
ptr=(cast-type*)malloc(byte-size);

Here, ptr is pointer of cast-type. The malloc() function returns


a pointer to an area of memory with size of byte size. If the
space is insufficient, allocation fails and returns NULL pointer.
malloc() cont.

Example:
int *ptr;
ptr=(int*)malloc(100*sizeof(int));
This statement will allocate either 200 or 400
according to size of int 2 or 4 bytes respectively and
the pointer points to the address of first byte of
memory.
Code Example of malloc()

Problem:

Write a C program to find sum of n


elements entered by user. To
perform this program, allocate
memory dynamically using
malloc() function.
calloc()
The name calloc stands for "contiguous allocation".
The difference between malloc() and calloc() is that, calloc()
zero-initializes the memory blocks, while malloc() leaves the
memory uninitialized.
Prototype:
void *calloc (no_of_blocks, size_of_each_block_in_bytes);

Syntax:
ptr=(cast-type*)calloc(no_of_blocks, size_of_each_block_in_bytes);

This statement will allocate contiguous space in memory for


an array of n elements.
calloc() cont.

Example:
float *ptr;
ptr=(float*)calloc(25, sizeof(float));
This statement allocates contiguous space in
memory for an array of 25 elements each of size of
float, i.e, 4 bytes.
Code Example of calloc()

Problem:

Write a C program to find


sum of n elements entered
by user. To perform this
program, allocate memory
dynamically using calloc()
function.
free()

Dynamically allocated memory with either calloc() or


malloc() does not get return on its own.
The programmer must use free() explicitly to release space
taken from the Heap.
Prototype:
void free(pointer to heap memory);

Example:
free(ptr);

This statement cause the space in memory pointer by ptr to


be deallocated.
realloc()

The realloc() function changes the size of a block of memory


that was previously allocated with malloc() or calloc().
If the previously allocated memory is insufficient or more
than sufficient. Then, you can change memory size
previously allocated using realloc().
Prototype:
void *realloc(void *ptr, size_t size);

The ptr argument is a pointer to the original block of


memory. The new size, in bytes, is specified by size.
Code Example of realloc()

You might also like