int main int a size i printfEnter array

  • Slides: 49
Download presentation

 הקצאת מערך בגודל משתנה int main() { int *a, size, i; printf("Enter array

הקצאת מערך בגודל משתנה int main() { int *a, size, i; printf("Enter array sizen"); scanf("%d", &size); main a size i Stack a = (int*) malloc(size * sizeof(int)); if (a == NULL) return 1; for (i = 0; i < size; i++) scanf("%d", &a[i]); for (i = 0; i < size; i++) printf("%d", a[i]); Heap free(a); } return 0; 12

 דוגמא - calloc int main() { int *a, size, i; printf("Enter array sizen");

דוגמא - calloc int main() { int *a, size, i; printf("Enter array sizen"); scanf("%d", &size); a = (int*) calloc(size, sizeof(int)); if (a == NULL) כל אלמנט , אלמנטים size הקצאת return 1; for (i = 0; i < size; i++) scanf("%d", &a[i]); sizeof(int) בגודל 0 - הפונקציה תאתחל את הזיכרון ל for (i = 0; i < size; i++) printf("%d", a[i]); free(a); } return 0; 14

 דוגמא - realloc int *ptr = NULL; int size = 0, new_size =

דוגמא - realloc int *ptr = NULL; int size = 0, new_size = 0; scanf("%d", &size); הקצאת זיכרון ptr = (int*) malloc( size * sizeof(int) ); /* incomplete, must check if allocation succeeded */. . . scanf("%d", &new_size); ptr = (int*) realloc( ptr, new_size*sizeof(int) ); /* incomplete, must check if allocation succeeded */ הקצאת חדשה . . . if (ptr != NULL) free(ptr); שחרור זיכרון 15

 זיכרון שהוקצה דינאמית int* func() { int *mem. Ptr = NULL; הזיכרון שייך

זיכרון שהוקצה דינאמית int* func() { int *mem. Ptr = NULL; הזיכרון שייך לתוכנית mem. Ptr = (int*) malloc(10 * sizeof(int)); ולא לפונקציה . . . מותר להחזיר כתובת return mem. Ptr; mem. Ptr דינאמית שהוקצה לזיכרון } int main() { int * ptr = func(); if (ptr != NULL) { // do something with ptr free(ptr); ptr = NULL; } ptr } 16

 קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index =

קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != 'n'; c = getchar()) { if (index == capacity – 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = ''; return buffer; } 18

 קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index =

קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; מערך בגודל התחלתי כלשהו for (c = getchar(); c != 'n'; c = getchar()) { if (index == capacity – 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT; } buffer[index++] = c; } buffer[index] = ''; return buffer; } 19

 קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index =

קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; יש לוודא שהקצאת הזיכרון לא נכשלה for (c = getchar(); c != 'n'; c = getchar()) { if (index == capacity – 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT; } buffer[index++] = c; } buffer[index] = ''; return buffer; } 20

 קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index =

קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; קריאת קלט מהמשתמש עד לסוף השורה for (c = getchar(); c != 'n'; c = getchar()) { if (index == capacity – 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT; } buffer[index++] = c; } buffer[index] = ''; return buffer; } 21

 קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index =

קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != 'n'; c = getchar()) { ? האם נגמר המקום if (index == capacity – 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT; } buffer[index++] = c; } buffer[index] = ''; return buffer; } 22

 קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index =

קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != 'n'; c = getchar()) { נקצה מערך גדול יותר if (index == capacity – 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT; } buffer[index++] = c; } buffer[index] = ''; return buffer; } 23

 קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index =

קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != 'n'; c = getchar()) { if (index == capacity – 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) לא לשכוח לבדוק return NULL; capacity = capacity * INCREMENT; } buffer[index++] = c; } buffer[index] = ''; return buffer; } 24

 קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index =

קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != 'n'; c = getchar()) { if (index == capacity – 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) capacity לא לשכוח לעדכן את return NULL; capacity = capacity * INCREMENT; } buffer[index++] = c; } buffer[index] = ''; return buffer; } 25

 קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index =

קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != 'n'; c = getchar()) { if (index == capacity – 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT; } buffer[index++] = c; נשמור את התו שקראנו במקום המתאים } buffer[index] = ''; return buffer; } 26

 קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index =

קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != 'n'; c = getchar()) { if (index == capacity – 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT; } buffer[index++] = c; } ‘’ בסוף נוסיף buffer[index] = ''; return buffer; } 27

 קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index =

קריאת שורת קלט בגודל לא ידוע char* read. Line() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; } for (c = getchar(); c != 'n'; c = getchar()) { if (index == capacity – 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT; } buffer[index++] = c; } – מחזירים זיכרון שהוקצה דינאמית buffer[index] = ''; return buffer; ( באחריות הלקוח לשחרר )צריך לתעד 28

read. Line שימוש ב int main() { char *line = read. Line(); קריאת שורת

read. Line שימוש ב int main() { char *line = read. Line(); קריאת שורת קלט מהמשתמש if (line == NULL) { printf("Fatal error: memory allocation failed!n"); return 1; } printf("%sn", line); free(line); return 0; } 29

read. Line שימוש ב int main() { char *line = read. Line(); אם הייתה

read. Line שימוש ב int main() { char *line = read. Line(); אם הייתה בעיה נדפיס הודעת שגיאה ונסיים if (line == NULL) { printf("Fatal error: memory allocation failed!n"); return 1; } printf("%sn", line); free(line); return 0; } 30

read. Line שימוש ב int main() { char *line = read. Line(); if (line

read. Line שימוש ב int main() { char *line = read. Line(); if (line == NULL) { printf("Fatal error: memory allocation failed!n"); return 1; המוחזר } printf("%sn", line); שימו לב לערך free(line); return 0; } 31

read. Line שימוש ב int main() { char *line = read. Line(); if (line

read. Line שימוש ב int main() { char *line = read. Line(); if (line == NULL) { printf("Fatal error: memory allocation failed!n"); return 1; } printf("%sn", line); נשתמש בקלט free(line); return 0; } 32

read. Line שימוש ב int main() { char *line = read. Line(); if (line

read. Line שימוש ב int main() { char *line = read. Line(); if (line == NULL) { printf("Fatal error: memory allocation failed!n"); return 1; } printf("%sn", line); free(line); נשחרר , כשאיננו צריכים יותר את הקלט את הזיכרון return 0; } 33

exit- דוגמה לשימוש ב realloc בעזרת int • נשנה את גודלו של מערך של

exit- דוגמה לשימוש ב realloc בעזרת int • נשנה את גודלו של מערך של נסיים את התכנית , • אם השינוי נכשל int* resize(int *array, int new. Size) { if (array == NULL) return array; array = (int*) realloc(array, new. Size * sizeof(int)); if (array == NULL) { printf("Fatal error: memory allocation failed!n"); exit(1); } return array; } 36

 הקצאה דינאמית של מבנים • בדיוק כמו בטיפוסים בסיסיים typedef struct { char

הקצאה דינאמית של מבנים • בדיוק כמו בטיפוסים בסיסיים typedef struct { char *title, *author; } Book; Book* new. Book(const char *title, const char *author) { Book *result = (Book*) malloc(sizeof(Book)); /* incomplete, must check if allocation succeeded */ result->title = (char*) malloc(strlen(title) + 1); /* incomplete, must check if allocation succeeded */ strcpy(result->title, title); result->author = (char*) malloc(strlen(author) + 1); /* incomplete, must check if allocation succeeded */ strcpy(result->author, author); return result; } 37

 מערך של מערכים int main() { int rows, cols, i; int** table; scanf("%d%d",

מערך של מערכים int main() { int rows, cols, i; int** table; scanf("%d%d", &rows, &cols); table = (int**) malloc(rows * sizeof(int*)); /* incomplete, must check if failed */ for (i = 0; i < rows; i++) { table[i] = (int*) malloc(cols * sizeof(int)); /* incomplete, must check if failed */ } init_table(table, rows, cols); print_table(table, rows, cols); for (i = 0; i < rows; i++) free(table[i]); free(table); return EXIT_SUCCESS; } 39

 איתחול והדפסה void init_table(int **table, int rows, int cols) { int i, j;

איתחול והדפסה void init_table(int **table, int rows, int cols) { int i, j; for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) table[i][j] = (i + 1) * (j + 1); } void print_table(int **table, int rows, int cols) { int i, j; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) printf("%4 d", table[i][j]); printf("n"); } } 40

 מצביעים לתוך מערך גדול int main() { int rows, cols, i; int* data;

מצביעים לתוך מערך גדול int main() { int rows, cols, i; int* data; int** table; scanf("%d%d", &rows, &cols); data = (int*) malloc(rows * cols * sizeof(int)); /* incomplete, must check if failed */ table = (int**) malloc(rows * sizeof(int*)); /* incomplete, must check if failed */ for (i = 0; i < rows; i++) table[i] = data + (cols * i); init_table(table, rows, cols); print_table(table, rows, cols); free(table); free(data); return EXIT_SUCCESS; } 41

 דוגמה • התכנית מדפיסה את ערכי המשתנים #include <stdio. h> #include <stdlib. h>

דוגמה • התכנית מדפיסה את ערכי המשתנים #include <stdio. h> #include <stdlib. h> int main(int argc, char *argv[]) { int i; for (i = 0; i < argc; i++) printf("argv[%d] = %sn", i, argv[i]); return EXIT_SUCCESS; } 45

 מחשבון פשוט int main(int argc, char *argv[]) { double left. Operand, right. Operand,

מחשבון פשוט int main(int argc, char *argv[]) { double left. Operand, right. Operand, result; char operator; /* incomplete, make sure all arguments are present */ left. Operand = atof(argv[1]); right. Operand = atof(argv[3]); operator = *argv[2]; switch (operator) { case '+': result = left. Operand break; case '-': result = left. Operand break; case '*': result = left. Operand break; case '/': result = left. Operand break; } + right. Operand; - right. Operand; * right. Operand; / right. Operand; printf("%g %c %g = %gn", left. Operand, operator, right. Operand, result); return EXIT_SUCCESS; } 48