mallocint size size int ip ip int malloc100sizeofint
- Slides: 43
פונקציות ספריה שימושיות malloc(int size) size מקצה שטח זיכרון רציף בגודל int *ip; ip = (int *) malloc(100*sizeof(int)); calloc(int num_elements, int element_size) . אבל גם מאפס את הזיכרון המוצבע malloc דומה ל int *ip; ip = (int *) calloc(100, sizeof(int)); – 2–
הקצאה דינמית של זיכרון 0 xffff User stack (created at runtime by function calls) Run-time heap (created at runtime by malloc) Read/write data Read-only code and data 0 – 4– אילו כתובות מוקצות ? (heap באופן דינמי )על ה
דוגמה להקצאה ושברור char *p 1 = malloc (3); char *p 2 = malloc (1); char *p 3 = malloc (4); free (p 2); char *p 4 = malloc (6); free (p 3); char *p 5 = malloc (2); p 2 p 1 – 15 –
דוגמה להקצאה ושברור char *p 1 = malloc (3); char *p 2 = malloc (1); char *p 3 = malloc (4); free (p 2); char *p 4 = malloc (6); free (p 3); char *p 5 = malloc (2); p 3 p 2 p 1 – 16 –
דוגמה להקצאה ושברור char *p 1 = malloc (3); char *p 2 = malloc (1); char *p 3 = malloc (4); free (p 2); char *p 4 = malloc (6); free (p 3); p 4 char *p 5 = malloc (2); p 3 p 2 p 1 – 17 –
דוגמה להקצאה ושברור char *p 1 = malloc (3); char *p 2 = malloc (1); char *p 3 = malloc (4); free (p 2); char *p 4 = malloc (6); free (p 3); p 4 char *p 5 = malloc (2); p 3 p 2 p 5 p 1 – 18 –
GC מספר אלגוריתמים קלאסיים ל Mark and sweep collection (Mc. Carthy, 1960) (compaction – ' לא מזיז בלוקים )אלא אם כן מבצעים 'הידוק n Reference counting (Collins, 1960) לא מזיז בלוקים n Copying collection (Minsky, 1963) מזיז בלוקים n : ספר העוסק בנושא Jones and Lin, “Garbage Collection: Algorithms for Automatic Dynamic Memory”, John Wiley & Sons, 1996. – 25 –
Mark and Sweep אלגוריתם void Garbage. Collect() { roots = Get. Roots(); // in C this is difficult for (int i = 0; i < roots. Count(); ++i) Mark(roots[i]); Sweep(); } – 28 –
Mark and Sweep (cont. ) DFS סימון של הגרף מבוסס על ptr mark(ptr p) { if (!is_ptr(p)) return; if (mark. Bit. Set(p)) return; set. Mark. Bit(p); for (i=0; i < length(p); i++) mark(p[i]); return; } // // // do nothing if not pointer check if already marked set the mark bit mark all children apply recursively . end עד p מפנה זיכרון בטווח Sweep ptr sweep(ptr p, ptr end) { while (p < end) { if mark. Bit. Set(p) clear. Mark. Bit(); else if (allocate. Bit. Set(p)) free(p); p += length(p); } } מביא אותנו לבלוק הבא – 30 – מנקה סימון של ה markbit משחרר בלוקים שהוקצו אבל לא סומנו markbit ב
. . . בפועל Garbage collection (Boehm-Weiser Conservative GC). באופן אוטומטי garbage collection מבצע n . . . כל מה שצריך לעשות זה n #include “gc. h” #define malloc(x) GC_malloc(x) #define calloc(n, x) GC_malloc((n)*(x)) #define realloc(p, x) GC_realloc((p), (x)) #define free(x) = GC_free(x) – 34 –
שגיאות אופייניות הקשורות לזיכרון Dereferencing bad pointers Reading uninitialized memory Overwriting memory Referencing nonexistent variables Freeing blocks multiple times Referencing freed blocks Failing to free blocks – 35 –
. . . שחרור זכרון יותר מפעם אחת x = malloc(N*sizeof(int)); <manipulate x> free(x); y = malloc(M*sizeof(int)); <manipulate y> free(x); – 43 –
התיחסות לבלוק ששוחרר x = malloc(N * sizeof(int)); <manipulate x> free(x); . . . y = malloc(M * sizeof(int)); for (i = 0; i < M; i++) y[i] = x[i]++; – 44 –
. . . שוב . שחרור חלקי בלבד של מבנה הנתונים struct list { int val; struct list *next; }; foo() { struct list *head = (struct list * ) malloc(sizeof(struct list)); head -> val = 0; head -> next = NULL; <create and manipulate the rest of the list>. . . free(head); return; } – 46 –
- Sum0
- Divideint
- Int max(int x int y)
- Inheritance calculator
- Public void drawsquare(int x, int y, int len)
- Const char *s
- Mainint
- Max int
- Int main(int argc, char** argv)
- Int main() int num=4
- Nnxn com
- Voidswap
- 7팩토리얼
- Int f (int n)
- Interface myinterface int foo(int x)
- Arduino constant int
- Const int size =18 string *tbl2
- Size of int
- Define the term size separation
- Int pin
- Int customer id
- Identify the y intercept
- Int *a[10]
- Esa multimedia.esa.int./multimedia/virtual-tour-iss
- Int main
- Int val
- Pointer and one dimensional array
- Include stdio.h
- Mapint
- Who.int
- Two parallel lines are _____ coplanar.
- Void main() int i j charval='j'
- Int i
- Int num
- Gasflaschen farbe
- Int management
- Public int id { get; set; }
- Struct node int data
- 1 char kaç byte
- A { private int x; public a( val) { x = val; } }
- #includestdio.h int main()
- Struct point int x y
- Int num
- Include