[go: up one dir, main page]

0% found this document useful (0 votes)
40 views25 pages

Dma PPT

The document discusses dynamic memory allocation in C. It explains static and dynamic memory allocation, and covers functions like malloc(), calloc(), free() and realloc() to allocate and manage memory dynamically at runtime. Examples are provided to illustrate how each function works.

Uploaded by

Shankar Gowri
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)
40 views25 pages

Dma PPT

The document discusses dynamic memory allocation in C. It explains static and dynamic memory allocation, and covers functions like malloc(), calloc(), free() and realloc() to allocate and manage memory dynamically at runtime. Examples are provided to illustrate how each function works.

Uploaded by

Shankar Gowri
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/ 25

Dynamic Memory Allocation

….
Contents
• Memory Allocation
Static and dynamic
• Dynamic memory allocation
1. malloc()
2. calloc()
3. free()
4. realloc()
1.Example on pointers
#include <stdio.h>
int main()
{
int c = 5;
int *p = &c;
printf("%d", *p);
return 0;
}
2.Example on pointers
int* p, c;
c = 5;
p = &c;
c = 1;
printf("%d", c);
printf("%d", *p);
3.Example on pointers
int* p, c, d;
c = 5;
d = -15;
p = &c;
printf("%d", *p);
p = &d;
printf("%d", *p);
Task
#include <stdio.h>
int main()
{
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
ptr = &x[2];
printf("*ptr = %d \n", *ptr);
printf("*ptr+1 = %d \n", *ptr+1);
printf("*ptr-1 = %d", *ptr-1);
}
4.Example on pointers
int* p, c;
c = 22;
printf("c: %d\n", &c);//address
printf("c: %d\n", c); //22
p = &c;
printf("p: %d\n", p); //address
printf(”p: %d\n", *p); //22
c = 11;
printf("p: %d\n", p); //add
printf("p: %d\n", *p); //11
*p = 2;
printf("c: %d\n", &c); //addr
printf("c: %d\n", c); //2
Memory allocation
• Memory allocation is the process of reserving a partial or complete portion of
computer memory for the execution of programs and processes.
• Memory allocation has two core types;
• Static Memory Allocation: Fixed memory
• Dynamic Memory Allocation: not fixed memory, it varies.
Example:

The procedure to change the size of an array is referred to as Dynamic


memory allocation in C.
Static STACK

char, int ,float


RAM

Memory Stack
allocation Two types Heap

Dynamic HEAP

Malloc, calloc
Dynamic Memory allocation
• C Dynamic Memory Allocation can be defined as a procedure in which
the size of a data structure (like Array) is changed during the runtime.
• C provides some functions to achieve these tasks. There are 4 library
functions provided by C defined under <stdlib.h> header file to facilitate
dynamic memory allocation in C programming. They are:
• malloc()
• calloc()
• free()
• realloc()
C malloc() method
• “malloc” or “memory allocation” method in C is used to
dynamically allocate a single large block of memory with the specified
size. It returns a pointer of type void which can be cast into a pointer
of any form.
• Syntax:
ptr = (cast-type*) malloc(byte-size)
Example:
Example :
// char pointer
char *cptr;
// allocate memory
cptr = (char *) malloc (5 * sizeof(char));
free() method
• “free” method in C is used to dynamically de-allocate the memory.
The memory allocated using functions malloc() and calloc() is not de-
allocated on their own. Hence the free() method is used, whenever the
dynamic memory allocation takes place. It helps to reduce wastage of
memory by freeing it.
• Syntax:
• free(ptr);
Example:
.

#include<stdlib.h>
int main() 1000 1050
{ 1004
2060
int *a; 1008 24 bytes of
a=(int *)malloc(6*sizeof(int)); P=null 1012 5566 memory
1000
*(a+0)=10;//50 4070
2000
*(a+1)=20; 888
*(a+3)=40;
999
Free(a);
} 7778

Stack(static) Heap(dynamic)
calloc()
• “calloc” or “contiguous allocation” method is used to dynamically
allocate the specified number of blocks of memory of the specified
type. It initializes each block with a default value ‘0’.
• Syntax:
• ptr = (cast-type*)calloc(n, element-size);

• For Example:
• ptr = (int*) calloc(5, sizeof(int));
.

#include<stdlib.h>
int main() 1000 10
{ 1004
20
int *a; 1008 24 bytes of
a=(int *)calloc(6,sizeof(int)); a 1012 40 memory
1000
*(a+0)=10; 0
2000
*(a+1)=20; 0
*(a+2)=40;
0
Free(a);
} 0

Stack(static) Heap(dynamic)
#include <stdio.h> Example
#include <stdlib.h>
int main()
{ int * p,n=5,i;
printf("Enter number of elements: %d\n", n);
p = (int*)calloc(n ,sizeof(int));
for (i = 0; i < n; ++i) {
p[i] = i + 1; //p[1]=1+1=2
P[2]=2+1=3
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", p[i]);
.
realloc()
• “realloc” or “re-allocation” method is used to dynamically change the
memory allocation of a previously allocated memory.
• In other words, if the memory previously allocated with the help of
malloc or calloc is insufficient, realloc can be used to dynamically re-
allocate memory.

• Syntax:
• ptr = realloc(ptr, newSize);

• where ptr is reallocated with new size 'newSize'.


Example
#include <stdio.h> n = 10;
#include <stdlib.h> printf("\n\nEnter the new size of the
.
int main() array: %d\n", n);
{ int * p,n=5,i; ptr = realloc(ptr, n * sizeof(int));
printf("Enter number of elements: for (i = 5; i < n; ++i) {
%d\n", n); ptr[i] = i + 1;}
p = (int*)calloc(n ,sizeof(int)); printf("The elements of the array are: ");
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
p[i] = i + 1; } printf("%d, ", ptr[i]);}
printf("The elements of the array are: "); free(ptr);
for (i = 0; i < n; ++i) { }
printf("%d, ", p[i]); return 0;
} } }
Lab syllabus
• Write a C program to find the largest element using Dynamic Memory
Allocation.
Questions expected:
1.What is pointer?
2.What are two types of memory allocation.
3.What is stack memory.
4.What is dynamic memory.
5.Mention the types of functions used for dynamic memory allocation.
6.What is free pointer
7.Explain malloc,calloc,realloc with example.
8.Difference between malloc and calloc.

You might also like