Intoduction to dynamic memory allocation

24
Title ~Contents~ ~Introduction to Dynamic Memory Allocation ~Static Memory Allocation ~Dynamic Memory Allocation ~Maloc, Calloc & Realloc~ ~Malloc ~Calloc ~Realloc ~Malloc and Calloc Difference

Transcript of Intoduction to dynamic memory allocation

Page 1: Intoduction to dynamic memory allocation

Title

~Contents~~Introduction to Dynamic Memory Allocation

~Static Memory Allocation~Dynamic Memory Allocation

~Maloc, Calloc & Realloc~~Malloc~Calloc

~Realloc~Malloc and Calloc Difference

Page 2: Intoduction to dynamic memory allocation

Title Variables

rollNo

float percentage;int rollNo;

2 byte

percentage

4 byte

Page 3: Intoduction to dynamic memory allocation

TitleArray Memory Allocation

Array Memory AllocationExample :-

int rollNo[10];

rollNo

20 byte

Page 4: Intoduction to dynamic memory allocation

TitleStatic Memory Allocation

Static Memory Allocation

But there are some issues with static memory allocation..!

Page 5: Intoduction to dynamic memory allocation

void main(){int rollNo[10], n, i;printf(“Number of students:”);scanf(“%d”, &n);for(i=0; i<n; i++){

scanf(“%d”, &rollNo[i] );}}

Output:Number of Students: 3101518

rollNo

10 15 18

Wastage of Memory

Page 6: Intoduction to dynamic memory allocation

void main(){int rollNo[10], n, i;printf(“Number of students:”);scanf(“%d”, &n);for(i=0; i<n; i++){

scanf(“%d”, &rollNo[i] );}}

Output:Number of Students: 60

rollNo

0 1 2 3 4 5 6 7 8 9

Lack of Memory

Page 7: Intoduction to dynamic memory allocation

TitleProblem with Static Memory Allocation

Static Memory Allocation

Can not change the size of allocated memory..!

Page 8: Intoduction to dynamic memory allocation

TitleDynamic Memory Allocation

Dynamic Memory Allocation

malloccallocrealloc

free

Page 9: Intoduction to dynamic memory allocation

Title malloc

malloc function is used to allocate memory blocks at run time.

Header File : #include<stdlib.h>Usage form : ptr = (ptr-type *) malloc(size-in-

byte);

Page 10: Intoduction to dynamic memory allocation

Title Example : malloc

void main(){int rollNo[6];

int *ptr;ptr =

}

(int *)

malloc

(6)rollNo

12 byte

6 byte

byte

;

Page 11: Intoduction to dynamic memory allocation

Title Example : malloc

void main(){int rollNo[6];

int *ptr;ptr =

}

(int *)

malloc

(12) ;

12 byte

Page 12: Intoduction to dynamic memory allocation

Title Example : malloc

void main(){int rollNo[6];

int *ptr;ptr =

}

(int *)

malloc

(12) ;

100

102

104

106

108

110

ptr

100

Page 13: Intoduction to dynamic memory allocation

Example : malloc

void main(){int i;int *ptr;ptr = (int *) malloc(12);for(i=0; i < 6 ; i++){

scanf(“%d”, ptr+i );}for(i=0; i<6; i++){

printf(“ %d ”, *(ptr+i) );}}

100

102

104

106

108

110

ptr

100

Output:

5 10 15 20 25 30

5 10 15 20 25 305 10 15 20 25 30

Page 14: Intoduction to dynamic memory allocation

Example : malloc

void main(){int i, n, *ptr;scanf(“%d”, &n);ptr = (int *) malloc( n*2 );for(i=0;i<n;i++){

scanf(“%d”, ptr+i );}for(i=0;i<n;i++){

printf(“ %d ”, *(ptr+i) );}}

210

212

214

216

ptr

210

20 34 54 11

Output:

20 34 54 11

420 34 54 11

Page 15: Intoduction to dynamic memory allocation

Title calloc

calloc function is used to allocate multiple memory blocks at run time.

Header File : #include<stdlib.h>Usage form : ptr = (ptr-type *) calloc(n,size);

Page 16: Intoduction to dynamic memory allocation

Example : malloc

void main(){int i, n, *ptr;scanf(“%d”, &n);ptr = (int *) malloc( n* sizeof(int) );for(i=0;i<n;i++){

scanf(“%d”, ptr+i );}for(i=0;i<n;i++){

printf(“ %d ”, *(ptr+i) );}}

6 byte

n = 3

Page 17: Intoduction to dynamic memory allocation

Example : malloc

void main(){int i, n, *ptr;scanf(“%d”, &n);ptr = (int *) calloc( n , sizeof(int) );for(i=0;i<n;i++){

scanf(“%d”, ptr+i );}for(i=0;i<n;i++){

printf(“ %d ”, *(ptr+i) );}}

n = 3

2 byte

3

Page 18: Intoduction to dynamic memory allocation

realloc function

int n, *ptr;scanf(“%d”, &n);

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

int a[5];

10 byte

ptr

n = 5

What if user want to store only two values after entering n=5..?

10 byte

a

Page 19: Intoduction to dynamic memory allocation

realloc function

int n, *ptr;scanf(“%d”, &n);

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

int a[n];

4 byte

ptr

n = 5

reallocExample:

realloc (ptr, 4);

10 byte

a

Page 20: Intoduction to dynamic memory allocation

realloc function

int n, *ptr;scanf(“%d”, &n);

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

int a[n];

8 byte

ptr

n = 5

reallocExample:

realloc (ptr, 8);

10 byte

a

Page 21: Intoduction to dynamic memory allocation

free function

void main(){int i, n, *ptr;scanf(“%d”, &n);ptr = (int *) malloc( n* sizeof(int) );for(i=0;i<n;i++){

scanf(“%d”, ptr+i );}for(i=0;i<n;i++){

printf(“ %d ”, *(ptr+i) );}}

10 byte

n = 5

freeExample:

free(ptr);

ptr

Page 22: Intoduction to dynamic memory allocation

free function

void main(){int i, n, *ptr;scanf(“%d”, &n);ptr = (int *) malloc( n* sizeof(int) );for(i=0;i<n;i++){

scanf(“%d”, ptr+i );}for(i=0;i<n;i++){

printf(“ %d ”, *(ptr+i) );}}

n = 5

freeExample:

free(ptr);

ptr

Page 23: Intoduction to dynamic memory allocation

TitleMalloc vs. CallocMalloc Calloc

The name malloc stands for memory allocation. The name calloc stands for contiguous allocation.

Allocates single block of storage space Allocates multiple blocks of storage

Syntax: void *malloc (size_in_bytes); Syntax: void *calloc (number_of_blocks, size_of_each_block_in_bytes);

Number of argument is 1 Number of arguments are 2

The contents of allocated memory are not changed. i.e., the memory contains unpredictable or garbage values. This presents a risk

The allocated region is 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.

Page 24: Intoduction to dynamic memory allocation

Title

Thank You

Do visit for more at www.codeitoff.blogspot.com