42
04 ררררררCopyright Meir Kalech 1 C programming Language Chapter 6 : Dynamic Memory Allocation (DMA)

ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

Embed Size (px)

Citation preview

Page 1: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 1

C programming Language

Chapter 6 :Dynamic Memory Allocation (DMA)

Page 2: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 2

What is Dynamic Memory Allocation?

The problem:Array definition: its size must be known at compilation time.The array, when used, may be either too small – not enough room for all the elements, or too big – so it’s a waste of memory.

The solution:Use Dynamic Memory Allocation (DMA):

create the array at run-time, after determining the required number of elements.

Dynamic memory allocation enables the programmer to: Request exactly the required amount of memory. Release the allocated memory when it is no longer needed.

Page 3: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 3

malloc Function malloc function enables to allocate memory at

run-time. Syntax:

malloc(number of requested bytes); Example:

int size;printf(“how many integers?”);scanf(“%d”, &size);malloc(size*sizeof(int));

Comment: All the allocation functions use the <alloc.h> library.

Page 4: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 4

calloc Function calloc function enables to allocate memory at run

time. The allocated memory is initialized (cleared) with zeros.

Syntax:calloc(number of elements,

size of each element);Example:

int size;printf(“how many integers?”);scanf(“%d”, &size);calloc(size, sizeof(int));

FINE!But I can’t access the allocated

memory?!

Page 5: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 5

Allocation malloc and calloc return the first address of the allocated

memory. Upon failure, they return NULL (0). We can save the return value in a pointer, and access the

memory by using the operator * or operator []. For instance:

int size;int *pointer;printf(“how many integers?”);scanf(“%d”, &size);pointer = malloc(size*sizeof(int));if(pointer != NULL)

pointer[0] = 54;

DON’T WORRY!!!

Pointers will help

you!

Page 6: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 6

Allocationpointer = malloc(size*sizeof(int));

Problem: What type does malloc return? int *, char *, float * ???

Answer: void * !!! void * may be changed to any pointer type by using casting.

pointer = (int *) malloc(size*sizeof(int));

Page 7: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 7

Heap Where is the memory allocated? Reminder: we studied about the “stack

segment” and “data segment”. Stack segment is dedicated to local variables. Allocated memory is not a local variable.

Conclusion: dynamic memory is allocated in the data segment (heap).

Data segment:Stack segment:

500p: 100 ?????500

int *p = (int *) malloc(5*sizeof(int));

Page 8: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 8

CommentBE CAREFUL: A pointer that points to allocated memory can be mistakenly

assigned to another memory address. In this case, we might lose our connection to the previously allocated memory.

Data segment:Stack segment:

500p: 100 ?????500

int *p = (int *) malloc(5*sizeof(int));

Page 9: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 9

CommentBE CAREFUL: A pointer that points to allocated memory can be mistakenly

assigned to another memory address. In this case, we might lose our connection to the previously allocated memory.

Data segment:Stack segment:

200p: 100 ?????500

???arr: 200NOW NOT ACCESSIBLE!!

int *p = (int *) malloc(5*sizeof(int));int arr[3];p = arr;

Page 10: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 10

CommentTo avoid this problem, we can first save the

address in another pointer:

Data segment:Stack segment:

200p: 100 ?????500

???arr: 200ACCESSIBLE through save

500

save: 300

int *save, *p = (int *) malloc(5*sizeof(int));int arr[3];save = p;p = arr;

Page 11: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 11

free Function Since dynamic memory is allocated in the

data segment, it is not deleted when the end of a block is reached, but it’s the programmer responsibility to explicitly delete it.

The free function gets the first address of an allocated memory, and frees it: Syntax:

free(first_address);

Page 12: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 12

free Function

Data segment:Stack segment:

500p1: 100 ?????500

int *p1, *p2;p1 = (int *) malloc(5*sizeof(int));p2 = (int *) malloc(3*sizeof(int));free(p2);free(p1);

300p2: 200 300???

Page 13: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 13

free Function

Data segment:Stack segment:

500p1: 100

int *p1, *p2;p1 = (int *) malloc(5*sizeof(int));p2 = (int *) malloc(3*sizeof(int));free(p2);free(p1);

300p2: 200

Page 14: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 14

free Function

Data segment:Stack segment:

500p1: 100

300p2: 200

BE CAREFUL!!!Avoid freeing

memory that is already freed

(execution error)

int *p1, *p2;p1 = (int *) malloc(5*sizeof(int));p2 = (int *) malloc(3*sizeof(int));free(p2);free(p1);free(p1);

Page 15: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 15

free Function

Data segment:Stack segment:

0p1: 100

300p2: 200

int *p1, *p2p1 = (int *) malloc(5*sizeof(int));p2 = (int *) malloc(3*sizeof(int));free(p2);free(p1);p1=NULL;free(p1);

Initialize your pointers with

NULL!Now, there is

no BUG.

Page 16: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 16

String Allocation For array allocation, the programmer gets the

size from the user. On the other hand, for string allocation, it

doesn’t make sense to ask the user for the size, since the meaning of a string size is its number of letters.

Therefore, it is common to define a buffer (of length fit for a large string) to store the input string and allocate memory according to the size of the input string.

Page 17: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 17

String Allocation

Data segment:Stack segment:

?p1: 100

?????…?

char *p1, buffer[30]; printf(“enter string”);scanf(“%s”, buffer);p1 = (char *) malloc(strlen(buffer)+1);strcpy(p1, buffer);free(p1);

buffer:200

Page 18: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 18

char *p1, buffer[30]; printf(“enter string”);scanf(“%s”, buffer);p1 = (char *) malloc(strlen(buffer)+1);strcpy(p1, buffer);free(p1);

String Allocation

Data segment:Stack segment:

?p1: 100

zion\0…?buffer:200

Input: “zion”

Page 19: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 19

char *p1, buffer[30]; printf(“enter string”);scanf(“%s”, buffer);p1 = (char *) malloc(strlen(buffer)+1);strcpy(p1, buffer);free(p1);

String Allocation

Data segment:Stack segment:

500p1: 100

zion\0…?buffer:200

?????500

Page 20: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 20

char *p1, buffer[30]; printf(“enter string”);scanf(“%s”, buffer);p1 = (char *) malloc(strlen(buffer)+1);strcpy(p1, buffer);free(p1);

String Allocation

Data segment:Stack segment:

500p1: 100

zion\0…?buffer:200

zion\0500

Page 21: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 21

char *p1, buffer[30]; printf(“enter string”);scanf(“%s”, buffer);p1 = (char *) malloc(strlen(buffer)+1);strcpy(p1, buffer);free(p1);

String Allocation

Data segment:Stack segment:

500p1: 100

zion\0…?buffer:200

Page 22: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 22

realloc Function One of the goals of dynamic memory allocation is to enable

reallocation, namely, increase/decrease the old allocation size. There is reallocation function called realloc. Example:

int size, size2;int *pointer;printf(“how many integers?”);scanf(“%d”, &size);pointer = (int *) malloc(size*sizeof(int));if(pointer == NULL)

exit(1);printf(“how many integers more?”);scanf(“%d”, &size2);pointer = (int *) realloc(pointer,(size+size2)*sizeof(int));if(pointer == NULL)

exit(1);

Page 23: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 23

realloc FunctionSome Comments:1. The new size is the old + additional size.2. The process:

If there is enough space • extend the old allocation.

Else• Allocate new size (old + additional).• Copy the old data to the new place. • Free the old allocation (should not be used anymore).

Return the first address of the allocation.3. If the first parameter is NULL, then just malloc.

Page 24: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 24

Array of Pointers The problem:

Assume we want to read a list of 3 names.The names differ in length but the maximum name length is 23.

First solution:char arr[3][23];

A list of names is an array of strings. Since a string is an array of char, we have a 2D array of char. Can cause waste of memory!!!

Snowwhite\0

Littleredridinghood\0

Cinderella\0

100123146

Page 25: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 25

Array of Pointers Second solution:

char *arr[3];

arr is array of 3 elements, where each of them is a pointer to char.Now each element can point to a different char array of a different size.

Snowwhite\0200

340

730

Little red riding hood\0

Cinderella\0

200

340

730

arr: 100

Page 26: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 26

Pointer to Pointer Sometimes the number of rows

(names) is unknown ahead of time. In this case, we should allocate both

the rows as well as the elements of each vector:1. Allocate array of pointers.2. Allocate each pointer in the array.

Page 27: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 27

Pointer to Pointer

char **ppChar = NULL, buffer[30];int num;0

ppChar: 100

?

num: 120

?????…?buffer:900

Page 28: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 28

Pointer to Pointerchar **ppChar = NULL, buffer[30];int num;printf(“how many names?”);scanf(“%d”, &num);ppChar = (char **) calloc(num, sizeof(char *));

150

ppChar: 100

3

num: 120

INPUT: 30

0

0

150

00000…0buffer:900

Page 29: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 29

Pointer to Pointerchar **ppChar = NULL, buffer[30];int num,i;printf(“how many names?”);scanf(“%d”, &num);ppChar = (char **)

calloc(num, sizeof(char *));for(i=0; i<num; i++){ scanf(“%s”, buffer); ppChar[i] = (char *)

malloc(strlen(buffer)+1); strcpy(ppChar[i], buffer);}

150

ppChar: 100

3

num: 120

200

0

0

150

SNOW\0…0buffer:900

SNOW\0200

Page 30: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 30

Pointer to Pointer

150

ppChar: 100

3

num: 120

200

340

0

150

RED\0?…0buffer:900

SNOW\0200

RED\0340

char **ppChar = NULL, buffer[30];int num,i;printf(“how many names?”);scanf(“%d”, &num);ppChar = (char **)

calloc(num, sizeof(char *));for(i=0; i<num; i++){ scanf(“%s”, buffer); ppChar[i] = (char *)

malloc(strlen(buffer)+1); strcpy(ppChar[i], buffer);}

Page 31: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 31

Pointer to Pointer

150

ppChar: 100

3

num: 120

200

340

490

150

buffer:900

SNOW\0200

RED\0340

490 CINDERELLA\0

CINDERELLA\0…?

char** ppChar = NULL, buffer[30];int num,i;printf(“how many names?”);scanf(“%d”, &num);ppChar = (char **)

calloc(num, sizeof(char *));for(i=0; i<num; i++){ scanf(“%s”, buffer); ppChar[i] = (char *)

malloc(strlen(buffer)+1); strcpy(ppChar[i], buffer);}

Page 32: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 32

Pointer to Pointer - freefor(i=0; i<num; i++) free(ppChar[i]);

150

ppChar: 100

3

num: 120

200

340

490

150

buffer:900

SNOW\0200

RED\0340

490CINDERELLA\0

CINDERELLA\0…?

Page 33: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 33

Pointer to Pointer - freefor(i=0; i<num; i++) free(ppChar[i]);

150

ppChar: 100

3

num: 120

200

340

490

150

buffer:900 CINDERELLA\0…?

Page 34: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 34

Pointer to Pointer - freefor(i=0; i<num; i++) free(ppChar[i]);free(ppChar);

150

ppChar: 100

3

num: 120

buffer:900 CINDERELLA\0…?

Page 35: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 35

Dynamic Memory Allocation - Function

How to allocate memory in a function? Problem: indeed DMA lifetime does not

depend on the function, but the scope and lifetime of the pointer to the memory is only in the function!

So how to use the allocated memory also outside the function?

Solution: Return the pointer from the function. Pass the pointer by address.

Page 36: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 36

Problemvoid func(){

int *p;p = (int *) calloc(3,sizeof(int));

}void main(){

func();// how to use p here???

}

100

HEAP:

000

STACK:

main

Page 37: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 37

Return Addressint *func(){

int *p;p = (int *) calloc(3,sizeof(int));return p;

}void main(){

int *pm = func();}

main

HEAP:

000

STACK:

pm: 250 100

100

Page 38: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 38

Pass Addressvoid func(int *p){

p = (int *) calloc(3,sizeof(int));

}void main(){

int* pm = NULL;func(pm);// how to use p here???

} main

HEAP:

STACK:

pm: 250 0

Page 39: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 39

Pass Addressvoid func(int *p){

p = (int *) calloc(3,sizeof(int));

}void main(){

int *pm = NULL;func(pm);// how to use p here???

}

func

HEAP:

STACK:

mainpm: 250 0

p: 400 0

Page 40: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 40

Pass Addressvoid func(int *p){

p = (int *) calloc(3,sizeof(int));

}void main(){

int *pm = NULL;func(pm);

}

HEAP:

100

000

func

STACK:

mainpm: 250 0

p: 400 100

Page 41: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 41

Pass Address – Right Wayvoid func(int **p){

*p = (int *) calloc(3,sizeof(int));

}void main(){

int *pm = NULL;func(&pm);

}

HEAP:

func

STACK:

mainpm: 250 0

p: 400 250

Page 42: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)

ספטמבר 04 Copyright Meir Kalech 42

Pass Address – Right Wayvoid func(int **p){

*p = (int *) calloc(3,sizeof(int));

}void main(){

int *pm = NULL;func(&pm);

}

HEAP:

100

000

If a parameter (pointer) is changed in a function (“p =“), it must be passed by address. If its pointed value is changed(“p[i] =“), it can be passed by value.

func

STACK:

mainpm: 250 100

p: 400 250