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

Dynamic Memory Allocation

Uploaded by

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

Dynamic Memory Allocation

Uploaded by

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

Memory Allocation in C

Programming
Subject: Problem-Solving Using C
Raj K Baliyar Singh, Asst. Professor, Dept. CSE

Static memory allocation

Memory allocated during compile time is called static memory. The memory
allocated is fixed and can not be increased or decreased during run time.
If allocating memory for an array during compile time so the size of the array is
fixed during declaration. Which can not be increased or decreased at run time.
Wastage of memory if the values stored in an array are less than its size.

Dynamic memory allocation

The process of allocating memory at the time


of execution is called dynamic memory
allocation.
 Heap is the segment of memory where
dynamic memory allocation takes place.
 A heap is an area of memory where
memory is allocated or deallocated
without any order.
 There are certain built-in functions that
can help in allocating and deallocating
some memory space at run time.
Pointers play an important role in dynamic memory allocation because
pointers can only be used to access the allocated memory.
Built-in functions:
1. malloc()
2. calloc()
3. realloc()
4. free()
malloc()

It is a built-in function declared in stdlib.h header file. It is used to dynamically


allocate a single large block of contiguous memory as per the specified size.
Syntax: (void* )malloc(size_t size)
malloc function simply allocates a memory block according to the size specified in
the heap and on success, it returns a pointer (void pointer) pointing to the first
byte of the allocated memory else returns NULL.
size_t is defined in <stdlib.h> as an unsigned int.
void pointer:
A void pointer is a pointer that has no associated data type with it. A void pointer
can hold an address of any type and can be typecasted to any type.
int a = 10;
char b = 'x';
void* p = &a; // void pointer holds address of int
'a'
p = &b; // void pointer holds the address of char
'b'

Typecasting in C is the process of converting one data type to another data type
by the programmer using the casting operator during program design.
int x;
float y;
y = (float) x;

malloc allocates the memory requested by the user without knowing the type of
data to be stored inside the memory.

A void pointer can be typecasted to an appropriate type.

int *ptr = (int* )malloc(4);


malloc allocates 4 bytes of memory in the heap and the address of the first byte is
stored in the pointer.

// 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);
}

printf("\nDisplay Numbers: ");


for (int i = 0; i < n; i++)
{
printf("%d\n", *(ptr+i));
}

printf("\nAdd Number: ");


for (int i = 0; i < n; i++)
{
sum += *(ptr + i);
}
printf("\nSum = %d", sum);
// deallocating the memory
free(ptr);

return 0;
}

You might also like