0% found this document useful (0 votes)
5 views

Dynamic Memory allocation

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Dynamic Memory allocation

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1

DYNAMIC MEMORY ALLOCATION

Types of Memory Allocation:

Memory Allocation

Static Dynamic
Memory Allocation Memory Allocation

Static Memory Allocation Dynamic Memory Allocation


1. Static memory allocation is a method of Dynamic memory allocation is a method of
allocating memory, and once the memory is allocating memory, and once the memory is
allocated, it is fixed. allocated, it can be changed.

2. It is compile time allocation. It is run time allocation


3. In static memory allocation, it is not possible In dynamic memory allocation, the memory can
to resize after initial allocation. be minimized or maximize accordingly.
Ex:
An array is a collection of a fixed number of
values. Once the size of an array is declared, you
cannot change it.
4.In static memory, allocation execution is faster In dynamic memory, allocation execution is
than dynamic memory allocation. slower than static memory allocation.
5. In static memory allocation, cannot reuse the Dynamic memory allocation allows reusing the
unused memory. memory. The programmer can allocate more
memory when required. He can release the
memory when necessary.

➢ Dynamic memory allocation refers to managing system memory at runtime.


2

➢ To allocate memory dynamically, the following library functions are used:


• malloc()
• calloc()
• realloc()
• free()
➢ These functions are defined in the <stdlib.h> header file.

malloc()

➢ "malloc" stands for memory allocation.


➢ It reserves a block of memory of the specified number of bytes and returns a void pointer to the
first byte of the allocated space.
➢ It is a function, takes integer (no. of bytes to be allocated) as an input and returns a pointer as an
output.
➢ If there is no enough contiguous memory space then it returns NULL.
➢ Generally used in complex data structure like structure.

Syntax of malloc()

ptr = (castType*) malloc(byte-size);

Example
ptr = (float*) malloc(100 * sizeof(float));

The above statement allocates 400 bytes of memory. It's because the size of float is 4 bytes.
The pointer ptr holds the address of the first byte in the allocated memory.

Example
Ptr=(char*) malloc(6);

-allocates 6 bytes of memory


3

Example: Program to calculate the sum of n numbers entered by the user

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}

printf("Enter elements: ");


for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}

printf("Sum = %d", sum);

// deallocating the memory


free(ptr);

return 0;
}

calloc()
➢ The name "calloc" stands for contiguous allocation.
➢ The calloc() function allocates multiple blocks of memory of same size and initializes all bits to
zero.
➢ Generally used in array.

Syntax of calloc()

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

n=no. of blocks

size=each block size

Example:

ptr = (float*) calloc(25, sizeof(float));


4

-The above statement allocates contiguous space in memory for 25 elements of type float.

Difference between malloc() and calloc()

malloc() calloc()

malloc() function will create a single block of calloc() function can assign multiple blocks of
memory of size specified by the user. memory for a variable.

It takes one argument, i.e the size of block. It takes two arguments, i.e the no. of blocks and the
size of each block.

malloc() function contains garbage value. The memory block allocated by a calloc()
function is always initialized to zero.

malloc is faster than calloc. calloc takes little longer than malloc because of the
extra step of initializing the allocated memory by
zero. However, in practice the difference in speed is
very tiny and not recognizable.

free()

-This function releases the dynamically allocated space.

-This function is important when the storage is limited.

Syntax of free()
free(ptr);

-ptr is a pointer to a memory block ,which has already been created by malloc or calloc.

-Use of invalid pointer in the call may create problems.

-To release an array of elements, only need to release the pointer once. It is an error to attempt to release
elements individually.
5

realloc()

➢ If the dynamically allocated memory is insufficient or more than required, you can alter the size
of previously allocated memory using the realloc() function.

Syntax of realloc()

ptr = realloc(ptr, x);

Here, ptr is reallocated with a new size x and points to the first byte of the new memory block.

Example: program illustrates Dynamic memory allocation.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf("%d", &n1);

ptr = (int*) malloc(n1 * sizeof(int));

printf("Addresses of previously allocated memory: ");


for(i = 0; i < n1; ++i)
printf("%u\n",ptr + i);

printf("\nEnter the new size: ");


scanf("%d", &n2);

// rellocating the memory


ptr = realloc(ptr, n2 * sizeof(int));

printf("Addresses of newly allocated memory: ");


for(i = 0; i < n2; ++i)
printf("%u\n", ptr + i);

free(ptr);

return 0;
}

Output:

Enter size: 2
Addresses of previously allocated memory:26855472
26855476
6

Enter the new size: 4


Addresses of newly allocated memory:26855472
26855476
26855480
26855484

Questions:

1. What is meant by dynamic memory allocation? Differentiate between malloc and calloc.
2. Write a program to input and print text using Dynamic Memory Allocation.
3. Write a program to read and print the student details using structure and Dynamic Memory Allocation.

4. What is the purpose of realloc( )?Explain with example.

You might also like