Presentation is loading. Please wait.

Presentation is loading. Please wait.

הקצאות דינאמיות קרן כליף.

Similar presentations


Presentation on theme: "הקצאות דינאמיות קרן כליף."— Presentation transcript:

1 הקצאות דינאמיות קרן כליף

2 ביחידה זו נלמד: מוטיבציה להקצאות דינאמיות מהי הקצאה דינאמית
יצירת מערך בגודל שאינו ידוע מראש החזרת מערך מפונקציה הקצאת מערך של מערכים הגדלת מערך הפונקציה strdup דוגמאות משולבות של מבנים עם הקצאות דינאמיות

3 מוטיבציה להקצאה דינאמית
בעזרת הקצאה דינאמית נוכל: להקצות מערך בזמן ריצה, בלי לדעת את גודלו בזמן קומפילציה נוכל להחזיר מערך מפונקציה

4 מהי הקצאה דינאמית? הקצאה דינאמית היא הקצאת שטח זיכרון בגודל מבוקש בזמן ריצת התוכנית בניגוד להקצאה סטטית שמוקצית בתחילת התוכנית וגודלה ידוע כבר בזמן קומפילציה הקצאה דינאמית מוקצית על שטח הזיכרון heap בניגוד להקצאה סטטית שמוקצית על ה- stack של הפונקציה ה- heap הוא שטח זיכרון המשותף לכל הפונקציות, בניגוד ל- stack בהקצאת זיכרון דינאמית המתכנת מבקש ממערכת ההפעלה זיכרון בגודל מסוים המוגדר בבתים ומקבל את כתובת הבית הראשון בקטע הזיכרון שקיבל

5 הגדרה נוספת ל- <type>*
אנו יודעים שמשתנה מטיפוס *<type>מכיל כתובת של משתנה מטיפוס <type> כלשהו כתובת התחלה של מערך מטיפוס <type>היא גם כתובתו של האיבר הראשון במערך, שהוא משתנה מטיפוס <type> לכן משתנה מטיפוס <type>* יכול להכיל גם כתובת התחלה של מערך לכן נאמר ש- <type>* הוא פוטנציאל למערך כלומר, יכול להכיל כתובת התחלה של מערך (ולא רק כתובת של משתנה יחיד מטיפוס <type>)

6 פונקציות לשימוש בהקצאות דינאמיות
כדי לעבוד עם הקצאות דינאמיות יש להכליל את הספריה stdlib.h לצורך שימוש בפונקציות הבאות: void* malloc(size_t size) הפונקציה מקבלת את גודל הזיכרון המבוקש בבתים ומחזירה כתובת לתחילת שטח הזיכרון שהוקצה, ו-NULL במקרה שלא היה מספיק זיכרון פנוי להקצאה נשים לב ש- size_t הוא typedef ל- int, כלומר הפונקציה מקבלת את מספר הבתים שעליה להקצות הפונקציה מחזירה void* שזה הכללה למצביע מכל טיפוס שטח הזיכרון המתקבל מכיל זבל void* calloc(size_t n,size_t size_el) כמו malloc אבל מקבלת את כמות האיברים שרוצה להקצות, ומה הגודל של כל איבר. מקצה את השטח ומאפסת אותו. מקצה מערך של n איברים כל איבר בגודל size_el בתים, כל ביט מאותחל לאפס. קריאה מוצלחת תחזיר את כתובת ההתחלה של הזיכרון המוקצה , ו- NULL במקרה שלא היה מספיק זיכרון פנוי להקצאה

7 malloc – הקצאת מערך בגודל שאינו ידוע מראש
לצורך שימוש בהקצאות דנאמיות #include <stdio.h> #include <stdlib.h> void main() { int size, *arr, i; printf("Please enter the size of the array: "); scanf("%d", &size); arr = (int*)malloc(size*sizeof(int)); if (!arr) // (arr == NULL) --> allocaton didn't succeed printf("ERROR! Out of memory!\n"); return; } printf("Values in the array: "); for (i=0 ; i < size ; i++) printf("%d ", *(arr+i)); //*(arr+i) == arr[i] printf("\nPlease enter %d numbers: ", size); scanf("%d", &arr[i]); printf("%d ", arr[i]); printf("\n"); נשים לב שלמשתנה על ה- heap אין שם, אלא רק יש אליו הצבעה מאחת הפונקציות! int ??? 3000 3004 3008 int 1 3000 5 3004 8 3008 זיכרון ה- heap int:size ??? 1000 int*: arr 1004 int: i 1008 int:size 3 1000 int*: arr 3000 1004 int: i ??? 1008 int:size 3 1000 int*: arr ??? 1004 int: i 1008 הזיכרון של ה- main

8 calloc – הקצאת מערך בגודל שאינו ידוע מראש
#include <stdio.h> #include <stdlib.h> void main() { int size, *arr, i; printf("Please enter the size of the array: "); scanf("%d", &size); arr = (int*)calloc(size, sizeof(int)); if (!arr) // (arr == NULL) --> allocaton didn't succeed printf("ERROR! Out of memory!\n"); return; } printf("Values in the array: "); for (i=0 ; i < size ; i++) printf("%d ", *(arr+i)); //*(arr+i) == arr[i] printf("\nPlease enter %d numbers: ", size); scanf("%d", &arr[i]); printf("%d ", arr[i]); printf("\n"); int 3000 3004 3008 int 1 3000 5 3004 8 3008 זיכרון ה- heap int:size 3 1000 int*: arr 3000 1004 int: i ??? 1008 int:size ??? 1000 int*: arr 1004 int: i 1008 int:size 3 1000 int*: arr ??? 1004 int: i 1008 הזיכרון של ה- main

9 שחרור הזיכרון שהוקצה אחריות המתכנת לשחרר את כל זיכרון שהוקצה דינאמית
במילים אחרות, המתכנת אחראי להחזיר למערכת ההפעלה כל שטח זיכרון שביקש ממנה בזמן ריצה הסיבה: שטח ה- heap עליו מוקצות ההקצאות הדינאמיות מוגבל בשטחו ומשותף לכל התוכניות, ואז התוכנית הבאה שתבקש זיכרון עלולה לקבל NULL כי אנחנו לא החזרנו את הזיכרון שביקשנו בסיום העבודה... צריך לזכור: כאשר יש סביבת עבודה משותפת, צריך להתחשב גם באחרים (וזה שיעור חשוב בכלל לחיים ;-)) הקומפיילר לא מתריע על אי-שחרור הזיכרון ואין שום אינדיקציה לדעת זאת, לכן חייבים לשים לב!!

10 פונקציה לשחרור הקצאות דינאמיות
void free(void* ptr) הפונקציה מקבלת את כתובת ההתחלה של הזיכרון שברצוננו לשחרר הפונקציה מקבלת void* שזה הכללה למצביע מכל טיפוס גם פונקציה זו נמצאת ב- stdlib.h

11 free – שחרור זיכרון ??? 1 5 8 זיכרון ה- heap 3 3000 ??? ??? 3 ???
#include <stdio.h> #include <stdlib.h> void main() { int size, *arr, i; printf("Please enter the size of the array: "); scanf("%d", &size); arr = (int*)malloc(size*sizeof(int)); if (!arr) // (arr == NULL) --> allocaton didn't succeed printf("ERROR! Out of memory!\n"); return; } printf("Values in the array: "); for (i=0 ; i < size ; i++) printf("%d ", *(arr+i)); //*(arr+i) == arr[i] printf("\nPlease enter %d numbers: ", size); scanf("%d", &arr[i]); printf("%d ", arr[i]); printf("\n"); free(arr); int ??? 3000 3004 3008 int 1 3000 5 3004 8 3008 זיכרון ה- heap int:size 3 1000 int*: arr 3000 1004 int: i ??? 1008 int:size ??? 1000 int*: arr 1004 int: i 1008 int:size 3 1000 int*: arr ??? 1004 int: i 1008 הזיכרון של ה- main

12 תזכורת: החזרת מערך מפונקציה
ראינו כי כאשר מעבירים מערך לפונקציה, מעבירים רק את כתובת ההתחלה שלו, ולא עותק של כל המערך ובאופן דומה, כאשר מחזירים מערך שהוגדר בפונקציה, חוזרת כתובת ההתחלה שלו, ולא עותק של כל המערך הבעייתיות: כאשר יוצאים מהפונקציה שטח הזיכרון שלה משתחרר ויש לנו מצביע לזיכרון שנמחק... הפתרון: הקצאה דינאמית

13 תזכורת: הבעייתיות בהחזרת מערך מפונקציה - דוגמא
#define SIZE 3 int* readArray() { int arr[SIZE], i; printf("Please enter %d numbers: ", SIZE); for (i=0 ; i < SIZE ; i++) scanf("%d", &arr[i]); return arr; } void main() int* arr, i; arr = printf("The array is: \n"); printf("%d ", arr[i]); printf("\n"); int[]: arr 3 2000 5 2004 6 2008 int: i 2012 int[]: arr ??? 2000 2004 2008 int: i 2012 הזיכרון של readArray הקומפיילר נותן warning: returning address of local variable or temporary שפירושה שאנחנו מחזירים כתובת למשתנה בזיכרון שישתחרר readArray(); לעולם לא נחזיר מפונקציה כתובת של משתנה שהוגדר בה מקומית! int*: arr ??? 1000 int: i 1004 int*: arr 2000 1000 int: i ??? 1004 הזיכרון של ה- main

14 הפתרון: הקצאת המערך דינאמית
#include <stdio.h> #include <stdlib.h> #define SIZE 3 int* buildArray() { int i; int* arr = (int*)malloc(SIZE*sizeof(int)); if (!arr) printf("ERROR! Not enough memory!\n"); exit(1); // to exit the program immedietly. // use only when memory allocation fails } for (i=0 ; i < SIZE; i++) arr[i] = i+1; return arr; void main() int *arr, i; arr = printf("Values in the array: "); for (i=0 ; i < SIZE ; i++) printf("%d ", arr[i]); printf("\n"); free(arr); הפתרון: הקצאת המערך דינאמית אם אנחנו יוצאים מפונקציה שהקצתה דינאמית ולא שחררה, חובה להחזיר את כתובת ההתחלה של ההקצאה, כדי שנוכל לשחרר אותה בהמשך! אחרת כאשר נצא מהפונקציה כבר לא יהיה משתנה שיכיל את מיקום ההקצאה! int:i ??? 2000 int*: arr 3000 2004 int:i ??? 2000 int*: arr 2004 הזיכרון של buildArray buildArray(); int 1 3000 2 3004 3 3008 int ??? 3000 3004 3008 int*: arr 3000 1000 int: i ??? 1004 int*: arr ??? 1000 int: i 1004 הזיכרון של ה- main זיכרון ה- heap

15 הקצאה בתוך פונקציה אחריות שלנו כמתכנתים לשחרר את כל הזיכרון שהקצינו
יש לשים לב בייחוד במקרים בהם ההקצאה מתבצעת בפונקציה אחת, והשחרור צריך להיות בפונקציה אחרת!

16 החזרת מערך מפונקציה by pointer
למדנו שפונקציה יכולה להחזיר ערכים ע"י קבלת כתובת לעדכון התשובה למשל: int getSum(int arr[], int size); לעומת: void getSum(int arr[], int size, int* sum); באותו אופן ניתן גם להחזיר מערך שייוצר בתוך הפונקציה לא לשכוח להעביר את המערך כ- **

17 החזרת מערך מפונקציה by pointer - דוגמא
#include <stdio.h> #include <stdlib.h> void buildArray(int* arr, int size) { int i; arr = (int*)malloc(size*sizeof(int)); if (!arr) } printf("ERROR! Not enough memory!\n"); exit(1); // to exit the program immedietly. // use only when memory allocation fails for (i=0 ; i < size ; i++) arr[i] = i+1; void main() int size, *arr=NULL, i; printf("Please enter the size of the array: "); scanf("%d", &size); buildArray(arr, size); printf("Values in the array: "); printf("%d ", arr[i]); printf("\n"); free(arr); int ??? 3000 3004 3008 int 1 3000 2 3004 3 3008 זיכרון ה- heap int:size 3 2000 int: i ??? 2004 int*: arr 3000 2008 int:size 3 2000 int: i ??? 2004 int*: arr NULL 2008 הזיכרון של buildArray int:size ??? 1000 int*: arr 1004 int: i 1008 int:size 3 1000 int*: arr NULL 1004 int: i ??? 1008 int:size ??? 1000 int*: arr NULL 1004 int: i 1008 פה התוכנית תעוף... הזיכרון של ה- main

18 החזרת מערך מפונקציה by pointer - התיקון
#include <stdio.h> #include <stdlib.h> void buildArray(int** arr, int size) } int i; *arr = (int*)malloc(size*sizeof(int)); if (!*arr) printf("ERROR! Not enough memory!\n"); exit(1); // to exit the program immedietly. // use only when memory allocation fails { for (i=0 ; i < size ; i++) (*arr)[i] = i+1; void main() int size, *arr=NULL, i; printf("Please enter the size of the array: "); scanf("%d", &size); buildArray(&arr, size); printf("Values in the array: "); printf("%d ", arr[i]); printf("\n"); free(arr); int 1 3000 2 3004 3 3008 int ??? 3000 3004 3008 זיכרון ה- heap int:size 3 2000 int: i ??? 2004 int**: arr 1004 2008 הזיכרון של buildArray int:size 3 1000 int*: arr NULL 1004 int: i ??? 1008 int:size ??? 1000 int*: arr NULL 1004 int: i 1008 int:size 3 1000 int*: arr 3000 1004 int: i ??? 1008 int:size ??? 1000 int*: arr 1004 int: i 1008 הזיכרון של ה- main

19 הקצאת מערך של מערכים (1) כעת אנחנו יכולים לייצר מטריצה שבכל שורה יש מספר שונה של איברים

20 הקצאת מערך של מערכים (2) #include <stdio.h> #include <stdlib.h> void main() { int rows, **matrix, i, j, *sizes; printf("Enter number of rows in the matrix: "); scanf("%d", &rows); matrix = (int**)malloc(rows*sizeof(int*)); sizes = (int*)malloc(rows*sizeof(int)); for ( ; ; ) printf("Enter size of row #%d: ", i+1); scanf("%d", &sizes[i]); matrix[i] = (int*)calloc(sizes[i], sizeof(int)); for (j=0 ; j < sizes[i] ; j++) matrix[i][j] = (i+1)*10+j+1; } printf("The matrix is:\n"); for (i=0 ; i < rows ; i++) printf("%d ", matrix[i][j]); printf("\n"); free(sizes); for ( ; ; ) free(matrix[i]); free(matrix); int:rows 3 1000 int**: matrix 3000 1004 int: i 1008 int: j ??? 1012 int*: sizes 4000 1016 int:rows 3 1000 int**: matrix 3000 1004 int: i 1008 int: j ??? 1012 int*: sizes 4000 1016 int:rows 3 1000 int**: matrix 3000 1004 int: i ??? 1008 int: j 1012 int*: sizes 4000 1016 int:rows 3 1000 int**: matrix 3000 1004 int: i 2 1008 int: j ??? 1012 int*: sizes 4000 1016 int:rows 3 1000 int**: matrix 3000 1004 int: i 1 1008 int: j ??? 1012 int*: sizes 4000 1016 int:rows 3 1000 int**: matrix 3000 1004 int: i 1008 int: j ??? 1012 int*: sizes 4000 1016 int:rows 3 1000 int**: matrix 3000 1004 int: i 1 1008 int: j ??? 1012 int*: sizes 4000 1016 int:rows ??? 1000 int**: matrix 1004 int: i 1008 int: j 1012 int*: sizes 1016 int:rows 3 1000 int**: matrix 3000 1004 int: i 2 1008 int: j ??? 1012 int*: sizes 4000 1016 int:rows 3 1000 int**: matrix 3000 1004 int: i 1008 int: j ??? 1012 int*: sizes 4000 1016 int:rows 3 1000 int**: matrix 3000 1004 int: i ??? 1008 int: j 1012 int*: sizes 1016 int:rows 3 1000 int**: matrix ??? 1004 int: i 1008 int: j 1012 int*: sizes 1016 i=0 i < rows i++ הזיכרון של ה- main int 11 4050 12 4054 int 4050 4054 int* 4050 3000 ??? 3004 3008 int* 4050 3000 5000 3004 2200 3008 int* 4050 3000 5000 3004 ??? 3008 int* ??? 3000 3004 3008 int 21 5000 22 5004 23 5008 int 5000 5004 5008 int 31 2200 32 2224 33 2228 34 2232 int 2200 2224 2228 2232 int 2 4000 3 4004 ??? 4008 int 2 4000 3 4004 4 4008 int ??? 4000 4004 4008 int 2 4000 ??? 4004 4008 i=0 i < rows i++ זיכרון ה- heap

21 הגדלת מערך בדוגמא הבאה אנו קולטים מהמשתמש מספרים לתוך מערך, לא ידוע כמה איברים המשתמש יכניס כל פעם כאשר כבר אין מקום במערך צריך להגדיל אותו פי 2 האלגוריתם: קרא את האיבר החדש, אם 1- צא אם אין מקום במערך, הקצה מערך גדול פי 2 העתק למערך החדש את האיברים מהמערך הישן שחרר את המערך המקורי שנה את מצביע המערך להצביע למערך החדש

22 הגדלת מערך - הפלט

23 הגדלת מערך – הקוד זיכרון ה- heap -1 ??? 4 3 4300 5 ??? 4 3 4300 5 ???
void main() { int num, i; int physSize = 2, logicSize = 0; int* arr = (int*)calloc(physSize, sizeof(int)); int* tmp; printf("Please enter numbers, -1 to stop:\n"); while (1) scanf("%d", &num); if (num == -1) break; if (physSize == logicSize) physSize *= 2; tmp = (int*)calloc(physSize, sizeof(int)); for (i=0 ; i < logicSize ; i++) tmp[i] = arr[i]; free(arr); arr = tmp; printf("Doubled the array size to %d\n", physSize); } printf("Read number is %d\n", num); arr[logicSize] = num; logicSize++; printf("The array has %d elements (physSize=%d):\n", logicSize, physSize); printf("%d ", arr[i]); printf("\n"); int 2200 int 7 2200 int 7 2200 4 int 4300 4304 4308 4312 int 7 4300 4 4304 4308 4312 int 7 4300 4 4304 5 4308 4312 זיכרון ה- heap int: num -1 1000 int: i ??? 1004 int: physSize 4 1008 int: logicSize 3 1012 int*: arr 4300 1016 int*: tmp 1020 int: num 5 1000 int: i ??? 1004 int: physSize 4 1008 int: logicSize 3 1012 int*: arr 4300 1016 int*: tmp 1020 int: num 5 1000 int: i ??? 1004 int: physSize 4 1008 int: logicSize 2 1012 int*: arr 2200 1016 int*: tmp 4300 1020 int: num 5 1000 int: i ??? 1004 int: physSize 4 1008 int: logicSize 2 1012 int*: arr 2200 1016 int*: tmp 1020 int: num 5 1000 int: i ??? 1004 int: physSize 4 1008 int: logicSize 2 1012 int*: arr 4300 1016 int*: tmp 1020 int: num 4 1000 int: i ??? 1004 int: physSize 2 1008 int: logicSize 1 1012 int*: arr 2200 1016 int*: tmp 1020 int: num ??? 1000 int: i 1004 int: physSize 2 1008 int: logicSize 1012 int*: arr 1016 int*: tmp 1020 int: num ??? 1000 int: i 1004 int: physSize 1008 int: logicSize 1012 int*: arr 1016 int*: tmp 1020 int: num ??? 1000 int: i 1004 int: physSize 2 1008 int: logicSize 1012 int*: arr 2200 1016 int*: tmp 1020 int: num 7 1000 int: i ??? 1004 int: physSize 2 1008 int: logicSize 1012 int*: arr 2200 1016 int*: tmp 1020 int: num 4 1000 int: i ??? 1004 int: physSize 2 1008 int: logicSize 1012 int*: arr 2200 1016 int*: tmp 1020 int: num 7 1000 int: i ??? 1004 int: physSize 2 1008 int: logicSize 1 1012 int*: arr 2200 1016 int*: tmp 1020 int: num 5 1000 int: i ??? 1004 int: physSize 2 1008 int: logicSize 1012 int*: arr 2200 1016 int*: tmp 1020 הזיכרון של ה- main

24 הפונקציה realloc שפת C נותנת לנו פונקציה שיודעת להגדיל מערך:
הפונקציה יודעת להקצות מקום חדש בגודל המבוקש ולהעתיק אליו את האיברים מהמערך הישן, ובסוף גם לשחרר את הזיכרון של המערך המקורי void* realloc(void* ptr, size_t size; ( הפונקציה מקבלת כפרמטר את כתובת ההתחלה של המערך שאותו ברצונה להגדיל וכן את גודל שטח הזיכרון החדש שרוצים הפונקציה מחזירה את כתובת ההתחלה של השטח החדש שהוקצה יתכן וזו תהיה הכתובת המקורית, במידה והיה מספיק מקום להגדלה במקום המקורי בו המערך הוקצה

25 הגדלת מערך – הקוד בשימוש realloc
void main() { int num, i; int physSize = 2, logicSize = 0; int* arr = (int*)calloc(physSize, sizeof(int)); printf("Please enter numbers, -1 to stop:\n"); while (1) scanf("%d", &num); if (num == -1) break; if (physSize == logicSize) physSize *= 2; arr = (int*)realloc(arr, physSize*sizeof(int)); printf("Doubled the array size to %d\n", physSize); } printf("Read number is %d\n", num); arr[logicSize] = num; logicSize++; printf("The array has %d elements (physSize=%d):\n", logicSize, physSize); for (i=0 ; i < logicSize ; i++) printf("%d ", arr[i]); printf("\n"); free(arr); int 7 2200 4 int 2200 int 7 2200 int 7 4300 4 4304 4308 4312 int 7 4300 4 4304 5 4308 4312 זיכרון ה- heap int: num 5 1000 int: i ??? 1004 int: physSize 4 1008 int: logicSize 2 1012 int*: arr 2200 1016 int: num 5 1000 int: i ??? 1004 int: physSize 2 1008 int: logicSize 1012 int*: arr 2200 1016 int: num 5 1000 int: i ??? 1004 int: physSize 4 1008 int: logicSize 2 1012 int*: arr 4300 1016 int: num -1 1000 int: i ??? 1004 int: physSize 4 1008 int: logicSize 3 1012 int*: arr 4300 1016 int: num 4 1000 int: i ??? 1004 int: physSize 2 1008 int: logicSize 1012 int*: arr 2200 1016 int: num 5 1000 int: i ??? 1004 int: physSize 4 1008 int: logicSize 3 1012 int*: arr 4300 1016 int: num ??? 1000 int: i 1004 int: physSize 2 1008 int: logicSize 1012 int*: arr 1016 int: num ??? 1000 int: i 1004 int: physSize 1008 int: logicSize 1012 int*: arr 1016 int: num ??? 1000 int: i 1004 int: physSize 2 1008 int: logicSize 1012 int*: arr 2200 1016 int: num 7 1000 int: i ??? 1004 int: physSize 2 1008 int: logicSize 1012 int*: arr 2200 1016 int: num 4 1000 int: i ??? 1004 int: physSize 2 1008 int: logicSize 1 1012 int*: arr 2200 1016 int: num 7 1000 int: i ??? 1004 int: physSize 2 1008 int: logicSize 1 1012 int*: arr 2200 1016 הזיכרון של ה- main

26 מה יהיה פלט התוכנית הבאה?
#include <stdio.h> #include <stdlib.h> void main() { char* c = (char*)malloc (sizeof(char)*5); char** s = (char**)malloc (sizeof(char*)*3); int i; for( ; ; ) printf("enter word : "); gets(c); s[i] = c; for(i=0; i<3; i++) printf("strings : %s \n",s[i]); free(c); free(s); } char ??? 4300 4301 4302 4303 4304 char ‘h’ 4300 ‘i’ 4301 4302 ??? 4303 4304 char ‘b’ 4300 ‘y’ 4301 ‘e’ 4302 4303 ??? 4304 char ‘g’ 4300 ‘o’ 4301 4302 ‘d’ 4303 4304 i=0 i < 3 i ++ char* 4300 5300 5304 ??? 5308 char* 4300 5300 5304 5308 char* 4300 5300 ??? 5304 5308 char* ??? 5300 5304 5308 זיכרון ה- heap char*: c ??? 1000 char**: s 1004 int: i 1008 char*: c 4300 1000 char**: s 5300 1004 int: i 3 1008 char*: c 4300 1000 char**: s 5300 1004 int: i 2 1008 char*: c 4300 1000 char**: s ??? 1004 int: i 1008 char*: c 4300 1000 char**: s 5300 1004 int: i 1008 char*: c 4300 1000 char**: s 5300 1004 int: i ??? 1008 char*: c 4300 1000 char**: s 5300 1004 int: i 1 1008 הזיכרון של ה- main

27 התיקון לתוכנית הקודמת זיכרון ה- heap 4300 5300 1 6300 5300 1 6300 5300
void main() { char* c; char** s = (char**)malloc (sizeof(char*)*2); int i; for( ; ; ) c = (char*)malloc (sizeof(char)*5); printf("enter word : "); gets(c); s[i] = c; for(i=0; i<2; i++) printf("strings : %s \n",s[i]); for (i=0 ; i < 2 ; i++) free(s[i]); free(s); } char ??? 4300 4301 4302 4303 4304 char ‘h’ 4300 ‘i’ 4301 4302 ??? 4303 4304 char ‘b’ 6300 ‘y’ 6301 ‘e’ 6302 6303 ??? 6304 char ??? 6300 6301 6302 6303 6304 i=0 i < 2 i ++ char* 4300 5300 ??? 5304 char* 4300 5300 6300 5304 char* ??? 5300 5304 זיכרון ה- heap char*: c 4300 1000 char**: s 5300 1004 int: i 1 1008 char*: c 6300 1000 char**: s 5300 1004 int: i 1 1008 char*: c 6300 1000 char**: s 5300 1004 int: i 2 1008 char*: c 4300 1000 char**: s 5300 1004 int: i 1008 char*: c ??? 1000 char**: s 5300 1004 int: i 1008 char*: c ??? 1000 char**: s 1004 int: i 1008 char*: c ??? 1000 char**: s 5300 1004 int: i 1008 הזיכרון של ה- main

28 char* strdup(const char *str)
מקבלת מחרוזת ומחזירה העתק שלה: מקצה דינאמית על ה- heap מערך של תווים בגודל המחרוזת המקורית, מעתיקה אליו את התוכן ומחזירה את כתובת ההתחלה שלו תחזיר NULL במידה וההקצאה נכשלה אחריות המתכנת לשחרר את המחרוזת שחזרה!!

29 זיכרון ה- static storage
strdup – דוגמא char ‘b’ 5300 ‘y’ 5301 ‘e’ 5302 ‘\0’ 5303 #include <stdio.h> #include <string.h> #include <stdlib.h> void main() } char str1[] = "hi"; char* str2 = "bye"; char* newStr1 = strdup(str1); char* newStr2 = strdup(str2); printf("The first duplicated string: |%s|\n", newStr1); printf("The second duplicated string: |%s|\n", newStr2); free(newStr1); free(newStr2); { זיכרון ה- static storage char ‘b’ 6300 ‘y’ 6301 ‘e’ 6302 ‘\0’ 6303 char ‘h’ 4300 ‘i’ 4301 ‘\0’ 4302 זיכרון ה- heap char: str1[] ‘h’ 1000 ‘i’ 1001 ‘\0’ 1002 char*: str2 5300 1003 char*: newStr1 4300 1007 char*: newStr2 6300 1011 char: str1[] ??? 1000 1001 1002 char*: str2 1003 char*: newStr1 1007 char*: newStr2 1011 char: str1[] ‘h’ 1000 ‘i’ 1001 ‘\0’ 1002 char*: str2 5300 1003 char*: newStr1 4300 1007 char*: newStr2 ??? 1011 char: str1[] ‘h’ 1000 ‘i’ 1001 ‘\0’ 1002 char*: str2 5300 1003 char*: newStr1 ??? 1007 char*: newStr2 1011 char: str1[] ‘h’ 1000 ‘i’ 1001 ‘\0’ 1002 char*: str2 ??? 1003 char*: newStr1 1007 char*: newStr2 1011 הזיכרון של ה- main

30 אין שום שינוי פרט לטיפוס ב- calloc/malloc
הקצאת מערך של מבנים #include <stdio.h> #include <stdlib.h> struct Point { int x, y; } typedef point_t; void main() int size, i; point_t* points; printf("How many points? "); scanf("%d", &size); points = (point_t*)calloc(size, sizeof(point_t)); if (!points) printf("ERROR! Out of memory!\n"); return; } printf("Points are: "); for (i=0 ; i < size ; i++) printf("(%d, %d) ", points[i].x, points[i].y); printf("\nPlease enter %d points: \n", size); printf("Point #%d: ", i+1); scanf("%d %d", &points[i].x, &points[i].y); printf("\n"); free(points) אין שום שינוי פרט לטיפוס ב- calloc/malloc

31 תרגיל הגדר מבנה המכיל שני שדות: תו ומספר
כתוב פונקציה המקבלת מחרוזת ומעדכנת בפרמטר פלט את מספר התווים השונים במחרוזת. בנוסף, הפונקציה תחזיר מערך מסוג המבנה שהוגדר ובו עבור כל תו במחרוזת יש לשמור את מספר המופעים שלו. במערך זה כל תו יופיע מקסימום פעם אחת בלבד הצג את המערך שהוחזר דוגמה: עבור המחרוזת xbxdcc פרמטר הפלט יתעדכן להיות 4 (כי יש 4 תווים שונים במחרוזת) והמערך שיוחזר יראה כך: c d b x 2 1

32 דרכים שונות להגדרת מערך
מערך של Student בגודל הידוע בזמן קומפילציה student_t arr[3]; מערך של Student בגודל שאינו ידוע בזמן קומפילציה scanf(“%d”, &size) student_t* arr = (student_t*)malloc(sizeof(student_t)*size); מערך של מצביעים ל- Student בגודל הידוע בזמן קומפילציה student_t* arr[3]; מערך של מצביעים ל- Student בגודל שאינו ידוע בזמן קומפילציה student_t** arr = (student_t**)malloc(sizeof(student_t*)*size);

33 דרכים להגדרת מערך (1) מערך של Student בגודל הידוע בזמן קומפילציה
student_t arr[3]; במקרה זה כל איברי המערך נמצאים על ה- stack מימוש זה בזבזני במידה ולא נשתמש בכל איברי המערך יעיל מבחינת ביצועים (אין הקצאות דינאמיות) student[]: arr[0]: name arr[0]: id arr[1]: name arr[1]: id arr[2]: name arr[2]: id

34 דרכים להגדרת מערך (2) מערך של Student בגודל שאינו ידוע בזמן קומפילציה
scanf(“%d”, &size) student_t* arr = (student_t*)malloc(sizeof(student_t)*size); במקרה זה רק כתובת ההתחלה של המערך נמצאת על ה- stack, בעוד המבנים עצמם נמצאים על ה- heap גם מימוש זה בזבזני במידה ולא נשתמש בכל איברי המערך יעיל מבחינת ביצועים (יש רק הקצאה אחת) student_t: name “yoyo” 2200 id 1111 2230 “gogo” 2236 2222 2246 student_t*: arr 2200 1000 int: size 2 1004

35 דרכים להגדרת מערך (3) מערך של מצביעים ל- Student בגודל הידוע בזמן קומפילציה student_t* arr[3]; במקרה זה יש מערך של 3 כתובות על ה- stack, והמבנים עצמם יוקצו דינאמית על ה- heap בעת הצורך, או יצביעו למבנים קיימים מימוש זה אופטימלי מבחינת מקום, כלומר נקצה מקום לנתוני המבנה רק בעת הצורך. פחות יעיל מבחינת ביצועים (כל אחד מהאיברים מוקצה דינאמית) student_t: name “yoyo” 2200 id 1111 student_t*[]: arr 2200 1000 5400 NULL student_t: name “yoyo” 5400 id 1111

36 דרכים להגדרת מערך (4) מערך של מצביעים ל- Student בגודל שאינו ידוע בזמן קומפילציה scanf(“%d”, &size); student_t** arr = (student_t**)malloc(sizeof(student_t*)*size); על ה- stack תהיה רק כתובת ההתחלה של מערך הכתובות מערך הכתובות יוקצה על ה- heap שכן גודלו אינו ידוע בזמן קומפילציה Student: name “yoyo” 2200 id 1111 Student**: arr 4400 1000 int: size 2 1004 Student**: arr ??? 1000 int: size 2 1004 Student: name “yoyo” 5400 id 1111 Student*[]: students 2200 4400 5400 Student*[]: students ??? 4400

37 מערך מבנים בתוך מבנה בדוגמא הבאה יש לנו את המבנה "כיתה" שמכיל מערך של MAX_STUDNETS סטודנטים בכל איבר במערך יהיו נתונים של סטודנט כלומר, גודל המערך קבוע - כמות הסטודנטים המקסימלית בכל כיתה זהה

38 דוגמא – כיתה עם סטודנטים
void printClass(class_t c) { int i; printf("The teacher is %s and the %d students are:\n", c.teacherName, c. registeredStudents); for (i=0 ; i < c. registeredStudents ; i++) printf(" %d- Name: %s\tId: %d\n", i+1, c.students[i].name, c.students[i].id); } void main() class_t c = {"Keren", 0}; char answer; int fContinue = 1; printf("Enter name and id for each student:\n"); do printf("Enter another student? "); flushall(); scanf("%c", &answer); if (answer == 'n' || answer == 'N') fContinue = 0; else printf("Student #%d: ", c.registeredStudents+1); scanf("%s %d", c.students[c.registeredStudents].name, &c.students[c.registeredStudents].id); c.registeredStudents++; } while (fContinue && c.registeredStudents < MAX_STUDENTS); printClass(c); } // main דוגמא – כיתה עם סטודנטים #include <stdio.h> #include <stdlib.h> #define MAX_STUDENTS 10 struct Student { char name[10]; int id; } typedef student_t; struct Class char teacherName[10]; int registeredStudents; student_t students[MAX_STUDENTS]; } typedef class_t;

39 החיסרונות כאשר גודל המערך קבוע
לא ניתן להגדיר כיתות בהן המספר המקסימלי של הסטודנטים שונה למשל עבור קורסים עם קבוצות לימוד קטנות, או עבור שיעור שהוא תרגול יתכן ו- numOfRegistered קטן משמעותית מ- MAX_STUDENTS ואז יש ביזבוז רב של מקום מבנה תופס יחסית הרבה מקום בזיכרון כי הוא מכיל אוסף של שדות

40 הקצאת מערך מבנים בתוך מבנה
בדוגמא הבאה יש לנו את המבנה "כיתה" שיכול להכיל מערך של תלמידים בכל איבר במערך יהיו נתונים של סטודנט מספר התלמידים המקסימלי אינו ידוע מראש וניתן ע"י המשתמש בזמן ריצה לכן מערך התלמידים שבתוך המבנה "כיתה" מוקצה דינאמית דוגמא, המשתמש בחר מערך בגודל 4, בכל איבר יהיו נתוני סטודנט

41 דוגמא – הקצאת סטודנטים בכיתה (הקוד בלבד, כך שאפשר לראות אותו )
struct Student { char name[10]; int id; } typedef student_t; struct Class char teacherName[10]; int numOfStudents; student_t* students; }typedef class_t; void printClass(class_t c) int i; printf("The teacher is %s and the %d students are:\n", c.teacherName, c.numOfStudents); for (i=0 ; i < c.numOfStudents ; i++) printf(" %d- Name: %s\tId: %d\n", i+1, c.students[i].name, c.students[i].id); } void main() { int i; class_t c = {"Keren"}; printf("How many students? "); scanf("%d", &c.numOfStudents); c.students = (student_t*)calloc(c.numOfStudents , sizeof(student_t)); // check if allocation succeeded.. printf("Enter name and id for each student:\n"); for (i=0 ; i < c.numOfStudents ; i++) printf("Student #%d: ", i+1); scanf("%s %d", c.students[i].name, &c.students[i].id); } printClass(c); free(c.students);

42 דוגמא – הקצאת סטודנטים בכיתה
#include <stdio.h> #include <stdlib.h> struct Student { char name[10]; int id; } typedef student_t; struct Class char teacherName[10]; int numOfStudents; student_t* students; }typedef class_t; void printClass(class_t c) { int i; printf("The teacher is %s and the %d students are:\n", c.teacherName, c.numOfStudents); for (i=0 ; i < c.numOfStudents ; i++) printf(" %d- Name: %s\tId: %d\n", i+1, c.students[i].name, c.students[i].id); } void main() class_t c = {"Keren"}; printf("How many students? "); scanf("%d", &c.numOfStudents); c.students = (student_t*)calloc(c.numOfStudents , sizeof(student_t)); // check if allocation succeeded.. printf("Enter name and id for each student:\n"); printf("Student #%d: ", i+1); scanf("%s %d", c.students[i].name, &c.students[i].id); printClass(c); free(c.students); int:i ??? 3000 class_t: char[10]:c.teacherName “keren” 3004 int: c.numOfStudents 2 student_t*:c.students 2200 הזיכרון של printClass שינוי שם המורה בתוך הפונקציה לא ישנה את הנתון המקורי, אבל שינוי ערכים במערך הסטודנטים כן ישתנו. student_t: name “yoyo” 2200 id 1111 2230 “gogo” 2236 2222 2246 student_t: name 2200 id 2230 2236 2246 זיכרון ה- heap int:i ??? 1000 class_t: char[10]:c.teacherName 1004 int: c.numOfStudents student_t*:c.students int:i ??? 1000 class_t: char[10]:c.teacherName “keren” 1004 int: c.numOfStudents student_t*:c.students int:i ??? 1000 class_t: char[10]:c.teacherName “keren” 1004 int: c.numOfStudents 2 student_t*:c.students int:i ??? 1000 class_t: char[10]:c.teacherName “keren” 1004 int: c.numOfStudents 2 student_t*:c.students 2200 הזיכרון של ה- main

43 אבל מה אם נרצה להקצות מערך ולהשתמש רק בחלק מהאיברים?
החיסרון במימוש המחזיק מערך של סטודנטים, בין אם הוקצה דינאמית או לא, הוא כאשר לא מנצלים את כל איברי המערך, ואז יש ביזבוז זיכרון לכן נקצה מערך של מצביעים למבנים בגודל המקסימלי שאנחנו רוצים, וכל איבר שיהיה בשימוש יצביע למבנה שיוקצה דינאמית

44 הקצאת מערך של מצביעים למבנים בתוך מבנה
בדוגמא הבאה יש לנו את המבנה "כיתה" שיכול להכיל אוסף של תלמידים מספר הסטודנטים המקסימלי ידוע מראש ויש מערך של מצביעים ל"סטודנט" בתחילה רשומים לכיתה 0 סטודנטים, וכל פעם נוסיף סטודנט נוסף לכיתה כל איבר יהיה מצביע ל"סטודנט". כל עוד לא נרשם סטודנט המצביע הוא NULL דוגמא: כיתה שיכולים להיות בה מקסימום 4 סטודנטים לאחר רישום סטודנט

45 דוגמא – רישום סטודנטים לכיתה
void main() { int i, fExit=0; char answer; class_t c = {"Keren”, 0}; do { if (c.registeredStudents == MAX_STUDENTS) printf("Class is full!\n"); break; } printf("Register a student (Y/N)? "); flushall(); scanf("%c", &answer); if (answer == 'N') fExit = 1; else c.students[c.registeredStudents] = (student_t*)calloc(1, sizeof(student_t)); printf("Enter name and id: "); scanf("%s %d", c.students[c.registeredStudents]->name, &c.students[c.registeredStudents]->id); c.registeredStudents++; } while (fExit==0); printClass(c); for (i=0 ; i < c.registeredStudents ; i++) free(c.students[i]); דוגמא – רישום סטודנטים לכיתה בדיקה אם יש מקום לסטודנט נוסף (הקוד בלבד, כך שאפשר לראות אותו ) #define MAX_STUDENTS 2 struct Student { char name[10]; int id; } typedef student_t; struct Class char teacherName[10]; int registeredStudents; student_t* students[MAX_STUDENTS]; }typedef class_t; void printClass(class_t c) int i; printf("The teacher is %s and the %d students are:\n", c.teacherName, c.registeredStudents); for (i=0 ; i < c.registeredStudents ; i++) printf(" %d- Name: %s\tId: %d\n", i+1, c.students[i]->name, c.students[i]->id); } פניה לאיבר הפנוי הבא בעזרת registeredStudents מערך של מצביעים פניה לשדות איברי המערך בעזרת <- (כי הם מצביעים)

46 דוגמא – רישום סטודנטים לכיתה
void main() { int i, fExit=0; char answer; class_t c = {"Keren"}; do { if (c.registeredStudents == MAX_STUDENTS) printf("Class is full!\n"); break; } printf("Register a student (Y/N)? "); flushall(); scanf("%c", &answer); if (answer == 'N') fExit = 1; else c.students[c.registeredStudents] = (student_t*)calloc(1, sizeof(student_t)); printf("Enter name and id: "); scanf("%s %d", c.students[c.registeredStudents]->name, &c.students[c.registeredStudents]->id); c.registeredStudents++; } while (fExit==0); printClass(c); for ( ; ; ) free(c.students[i]); class_t: char[10]:c.teacherName “Keren” 1000 int: c.registeredStudents student_t*[] :c.students NULL class_t: char[10]:c.teacherName “Keren” 1000 int: c.registeredStudents 1 student_t*[] :c.students 2200 5300 class_t: char[10]:c.teacherName “Keren” 1000 int: c.registeredStudents student_t*[] :c.students 2200 NULL class_t: char[10]:c.teacherName “Keren” 1000 int: c.registeredStudents 2 student_t*[] :c.students 2200 5300 class_t: char[10]:c.teacherName “Keren” 1000 int: c.registeredStudents 1 student_t*[] :c.students 2200 NULL class_t: char[10]:c.teacherName ??? 1000 int: c.registeredStudents student_t*[] :c.students הזיכרון החלקי של ה- main #include <stdio.h> #include <stdlib.h> #define MAX_STUDENTS 2 struct Student { char name[10]; int id; } typedef student_t; struct Class char teacherName[10]; int registeredStudents; student_t* students[MAX_STUDENTS]; }typedef class_t; void printClass(class_t c) int i; printf("The teacher is %s and the %d students are:\n", c.teacherName, c.registeredStudents); for (i=0 ; i < c.registeredStudents ; i++) printf(" %d- Name: %s\tId: %d\n", i+1, c.students[i]->name, c.students[i]->id); } Y student_t: name “momo” 2200 id 1111 student_t: name 2200 id student_t: name “gogo” 5300 id 2222 student_t: name 5300 id זיכרון ה- heap i=1 i=2 i=0 i=0 i < c.registeredStudents i++

47 הקצאת מערך של מצביעים למבנים בתוך מבנה
בדוגמא הבאה יש לנו את המבנה "כיתה" שיכול להכיל אוסף של תלמידים מספר הסטודנטים המקסימלי אינו ידוע מראש וניתן ע"י המשתמש בזמן ריצה בתחילה רשומים לכיתה 0 סטודנטים, וכל פעם נוסיף סטודנט נוסף לכיתה בכל איבר יהיה מצביע ל"סטודנט". כל עוד לא נרשם סטודנט המצביע הוא NULL דוגמא: כיתה שהמשתמש החליט שיכולים להיות בה מקסימום 4 סטודנטים: לאחר רישום סטודנט

48 דוגמא – רישום סטודנטים לכיתה דינאמית (1)
#include <stdio.h> #include <stdlib.h> struct Student { char name[10]; int id; } typedef student_t; struct Class char teacherName[10]; int numOfStudents; int registeredStudents; student_t** students; }typedef class_t; void printClass(class_t c) int i; printf("The teacher is %s and the %d students are:\n", c.teacherName, c.registeredStudents); for (i=0 ; i < c.registeredStudents ; i++) printf(" %d- Name: %s\tId: %d\n", i+1, c.students[i]->name, c.students[i]->id); } מערך בגודל שאינו ידוע עדיין ושכל איבר בו יהיה מצביע: כוכבית אחת כי זהו מערך שמוצקה דינאמית, כדי להכיל את כתובת תחילת המערך כוכבית שניה כי כל איבר במערך הוא כתובת

49 דוגמא – רישום סטודנטים לכיתה דינאמית (2)
void main() { int i, fExit=0; char answer; class_t c = {"Keren"}; printf("How many max students? "); scanf("%d", &c.numOfStudents); c.students = (student_t**)malloc(sizeof(student_t*)*c.numOfStudents); // check if allocation succeeded do { if (c.registeredStudents == c.numOfStudents) printf("Class is full!\n"); break; } printf("Register a student (Y/N)? "); flushall(); scanf("%c", &answer); if (answer == 'N') fExit = 1; else c.students[c.registeredStudents] = (student_t*)calloc(1, sizeof(student_t)); printf("Enter name and id: "); scanf("%s %d", c.students[c.registeredStudents]->name, &c.students[c.registeredStudents]->id); c.registeredStudents++; } while (fExit==0); printClass(c); for (i=0 ; i < c.registeredStudents ; i++) free(c.students[i]); free(c.students); דוגמא – רישום סטודנטים לכיתה דינאמית (2) קבלת מספר הסטודנטים המקסימלי מהמשתמש הקצאת מערך של מצביעים לסטודנט שיחרור כל אחד מאיברי המערך שיחרור מערך המצביעים שגם הוקצה דינאמית (כמו בדוגמא "מערך של מערכים")

50 השוואה בזכרון בין מערך מבנים למערך מצביעים
נניח כי מבנה Student תופס 16 בתים בזכרון נניח כי יש כיתה עם פוטנציאל ל- 100 סטודנטים, אבל בפועל רשומים רק 40 מערך מבנים מערך מצביעים גודל הזכרון למבנים 16x100 = 1600 16 x 40 = 640 גודל הזכרון למצביעים 4 x 100 = 400 סה"כ הזכרון שבשימוש 1600 1040 סה"כ הזכרון המבוזבז 16x60 = 960 4 x 60 = 240

51 ביחידה זו למדנו: מוטיבציה להקצאות דינאמיות מהי הקצאה דינאמית
יצירת מערך בגודל שאינו ידוע מראש החזרת מערך מפונקציה הקצאת מערך של מערכים הגדלת מערך הפונקציה strdup דוגמאות משולבות של מבנים עם הקצאות דינאמיות 51 © Keren Kalif


Download ppt "הקצאות דינאמיות קרן כליף."

Similar presentations


Ads by Google