1 / 29

Chapter 5 Dynamic Allocation

Chapter 5 Dynamic Allocation. Main() { int j; real *x; x=malloc(size of(*x)); scanf(”%d”,j); *x=func(j); printf(*x); free(x); exit(0); } real func(int j) { if (j<=0) return 0.0 else return func(j-1)+1.0; }. Memory Allocation:. When this program is executed,

munin
Download Presentation

Chapter 5 Dynamic Allocation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Dynamic Allocation Main() { int j; real *x; x=malloc(size of(*x)); scanf(”%d”,j); *x=func(j); printf(*x); free(x); exit(0); } real func(int j) { if (j<=0) return 0.0 else return func(j-1)+1.0; } Memory Allocation: When this program is executed, where are all the variables in the computer’s memory?

  2. Chapter 5 Dynamic Allocation Main() { int j; real *x; x=malloc(size of(*x)); scanf(”%d”,j); *x=func(j); printf(*x); free(x); exit(0); } real func(int j) { if (j<=0) return 0.0 else return func(j-1)+1.0; } Memory Allocation: Identify all the memory requirements. 1. j 2. x 3. func A recursive function call

  3. Chapter 5 Dynamic Allocation Main() { int j; real *x; x=malloc(size of(*x)); scanf(”%d”,j); *x=func(j); printf(*x); free(x); exit(0); } real func(int j) { if (j<=0) return 0.0 else return func(j-1)+1.0; } Memory Allocation: Static allocation Computer memory data Program 1 code ... Program 2 dynamic allocation heap stack

  4. Chapter 5 Dynamic Allocation Two types of errors in dynamic allocation: x=malloc(); … free(x); ... printf(*x); x=malloc(); ... printf(*x); … exit(); x will become garbage. dangling reference Need garbage collection mechanism Need operating system’s memory protection mechanism

  5. Chapter 5 Dynamic Allocation 5.1 Linked Lists Two types of Lists Array: Contiguous memory address Easy to scan an array, jump to the middle of an array. However, it is difficult to add an element to or delete an element from the middle of a contiguous data structure.

  6. Chapter 5 Dynamic Allocation 5.1 Linked Lists start Data Link Three basic operations: Insert: Delete: time complexities? Append:

  7. Chapter 5 Dynamic Allocation 5.1 Linked Lists Insert: at the beginning at the middle must first search to the right position start start start

  8. Chapter 5 Dynamic Allocation 5.1 Linked Lists delete start Don’t forget to return (free) it to memory. start

  9. Chapter 5 Dynamic Allocation 5.1 Linked Lists 5.1 Linked Lists start append start

  10. Chapter 5 Dynamic Allocation 5.1 Linked Lists Implement a linked structure using an array 1 3 4 10 Need a start link. I data[I] next[I] 0 3 6 1 * * 2 1 0 3 10 -1 4 * * 5 * * 6 4 3 How to insert, delete, and append? start end

  11. Chapter 5 Dynamic Allocation 5.1 Linked Lists Implement a linked structure using an array With a free list Free list 1 3 4 10 I data[I] next[I] 0 3 6 1 * 4 2 1 0 3 10 -1 4 * -1 5 * 1 6 4 3 Data_start Free_start end

  12. Chapter 5 Dynamic Allocation 5.1 Linked Lists If the maximum size of the list is not known, then we must use dynamically allocated linked structures. Example for simple database ADT Each record is something like: key data next Assume an ordered linked list in nondecreasing order.

  13. Chapter 5 Dynamic Allocation 5.1 Linked Lists How many pointers should find record return? If we want to insert here We must return there two pointers

  14. Chapter 5 Dynamic Allocation 5.1 Linked Lists Database record and index type declaration found Typedef struct dbrec { dbkeytype key; dbdatatype data; struct dbrec *next; } *Dbrec; Typedef struct { struct dbrec *it, *prior; } indextype; prior it Not found, insert prior it prior or it could be Null.

  15. Chapter 5 Dynamic Allocation 5.1 Linked Lists int findRec(Dbrec *db,dbkeytype key,dbdatatype *data, indextype *idx) /* The keys are in order */ { Dbrec rec=*db, prior=NULL; int cmp; boolean eor=false; if (rec==Null) { /* error handling */ printf(“findRec-empty list”); return -1; } while ( (!eof) && (cmp=comparekey(key,rec->key))>0) { prior=rec; if ((rec=rec->next) == Null) eof=true; } idx->it=rec; idx->prior=prior; if (!eof) { copydata(rec->data,*data); return 1; } return 0; }

  16. Chapter 5 Dynamic Allocation 5.1.2 Doubly-Linked Lists A node prev data next 1 3 4 10

  17. Chapter 5 Dynamic Allocation 5.1.2 Doubly-Linked Lists If a doubly-linked list is used to implement a simple data base, then the index returned by findRec() can consist of just a pointer. (to the record found or the record after (or before?) the insertion point) Either will do, but you have to take care of the situation of insertion before the first record or after the last record. Insertion and deletion are made more complicated by the fact that four pointers, instead of two, must be changed.

  18. Chapter 5 Dynamic Allocation 5.1.3 Circular Lists 1 3 4 10 When traversing a circular list we must recognize the end of the list so we do not go into an infinite loop. Inserting an element into an empty list must be done with care. The result will be a list of one element whose next pointer points back to itself.

  19. Chapter 5 Dynamic Allocation 5.1.3 Circular Lists Traversing a circular list Void eachElement(Listptr ptr, void (*fn)(Listdata)) { Listptr p=ptr; if (p) { do { (*fn) (p->data); p=p->next; } while (p != ptr); } }

  20. Chapter 5 Dynamic Allocation Summary Singly-linked list Doubly-linked list Circularly-linked list • Operations you must be able to code: • Initialize a list • Search for a record in the list • Insert a record into a list • Delete a record from the list • Modify a record in the list

  21. Chapter 5 Dynamic Allocation 5.2 Sparse Tables In many applications, the choice of a table seems to be the most natural one, but space considerations may preclude this choice. This is particularly true if only a small fraction of the table is actually used. A table of this type is called a sparse table. As an example, consider the problem of storing grades for all students in a university for a certain semester. Assume there are 8000 students and 300 classes.

  22. Chapter 5 Dynamic Allocation 5.2 Sparse Tables

  23. Chapter 5 Dynamic Allocation 5.2 Sparse Tables The entire table occupies (8000 students)x(300 classes)x(1 byte)=2.4 million bytes. Assuming that, on the average, students take four classes a semester, each column of the table would have only 4 cells occupied by grades, and the rest of the cells, 296 cells or 98.7% are unoccupied and wasted. This is a truly sparse table.

  24. Chapter 5 Dynamic Allocation 5.2 Sparse Tables A better approach, use two 2-dimensional arrays

  25. Chapter 5 Dynamic Allocation 5.2 Sparse Tables Assume that a student can take at most eight classes and that there can be at most 250 students signed up for a class. Then the space requirement: Classes Taken=8000x8x3 bytes=192000 bytes Students in Classes=300x250x3=225000 bytes Total: 417000 bytes, one-fifth of previous single table approach

  26. Chapter 5 Dynamic Allocation 5.2 Sparse Tables Still suffer from a wasteful use of space; seldom if ever will both arrays be full since most classes have less than 250 students and most students take less than 8 classes. The structure is also inflexible; if a class can be taken by more than 250 students, the data structure has to be modified.

  27. Chapter 5 Dynamic Allocation 5.2 Sparse Tables Use two one-dimensional arrays of linked lists

  28. Chapter 5 Dynamic Allocation 5.2 Sparse Tables Each record contain 5 fields: student number, class number, grade, a pointer to the next student and a pointer to the next class. Assume each pointer requires 2 bytes, each record occupies 9 bytes. The entire structure can be stored in (8000 students)x(4 classes in average)x(9 bytes)=288000 bytes, which is approximately 10% of the first implementation and about 70% of the second.

  29. Chapter 5 Dynamic Allocation Exercise: Programming Assignment Exercise 2 in Section 5.5 on Page 80.

More Related