Dma PPT
Dma PPT
….
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:
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);