Presentation is loading. Please wait.

Presentation is loading. Please wait.

Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.

Similar presentations


Presentation on theme: "Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec."— Presentation transcript:

1 Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec { char ssn[10]; char name[31]; }; char name[31]; }; And then we were to make the declaration: struct custrecrec customer[1000]; (Which requires 41-bytes of contiguous RAM) (Which requires 41 * 1,000 = 41,000 bytes of contiguous RAM) We might get the run-time error message: Not enough memory (Or something similar)

2 Page 2 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Well, That’s because there is not enough memory, Right??? Not necessarily. If we were to check, we might find that we actually have 250,000 bytes of ram available. Why Can’t we get the memory we need ?? If we were to look inside RAM, we might see: 401KB: Assigned 401KB: Assigned 36 KB Available 43 KB: Assigned 28 KB: Avail. 78 KB: Assigned 22 KB: Avail. 234 KB: Assigned 29 KB: Available 512 KB: Assigned 35 KB: Avail. 386 KB: Assigned 386 KB: Assigned 31 KB: Avail. 12 KB: Avail. 18 KB: Avail. 26 KB: Avail. 127 KB: Assigned 14 KB: Avail. 23 KB: Assigned 30 KB: Avail. 32 KB: Assigned 76 KB: Assigned

3 Page 3 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation To get the necessary memory we need NON-CONTIGUOUS storage. Let’s redefine our data type: struct custrec { char ssn[10]; char name[31]; struct custrec * next; }; How can we have a pointer within a structured object ?? struct Remember, when we defined a structured data object (struct), we noted that it could also include a pointer: next The variable (location in RAM) next will contain an address. next If we go that address (assume next contains the address 65100), we will find (on 45-bytes): (Which requires 45-bytes of contiguous RAM) 65100 - 65109 ssn 65110 - 65140 name 65141 - 65145 next Character array on 10-bytes Character array on 31-bytes Address Field on 4-bytes

4 Page 4 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation How would this work ??? Assume that we wished to keep track of the following records: ssnname ‘123456789’ ‘234567890’ ‘345678901’ ‘456789012’ ‘567890123’ ‘Chaucer, G.’ ‘Milton, J.’ ‘Browning, R.’ ‘Eliot, T.S.’ ‘Shelley, P.’ But if we were to look at available RAM we saw: Assigned 9200 - 9253 Available 9254 - 9302 Assigned 9303 - 9321 Available 9322 - 9417 Assigned 9404 - 9543 Available 9544 - 9598 Assigned 9599 - 9621 Available 9622 - 9774

5 Page 5 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation We know that our structured data object requires: struct custrec { char ssn[10];10-bytes char name[31]; }; 31-bytes 41-bytes 41-bytes of contiguous storage 205-bytes OR: 41 * 5 = 205-bytes of contiguous storage for the database IF we look at our available storage, however: Available Locations 9254 - 9302 Contiguous RAM Available 49 9322 - 941796 9544 - 959855 9622 - 9774153 353 Total Bytes Which is enough for our entire database, and even 1, 2, or 3 records contiguously, BUT NOT ALL 5 RECORDS CONTIGUOUSLY

6 Page 6 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Now, let’s redefine our data type as we did before: struct custrec { char ssn[10];10-bytes char name[31];31-bytes struct custrec * next; }; 4-bytes 45 bytes 45 bytes of contiguous storage We could set-up a linked-list to take advantage of our limitations: “123456789” 9254 - 9263 “Chaucer, G.” 9264 - 9294 9295 - 9298 9322 “234567890” 9322- 9331 “Milton, J.” 9332 - 93609361- 9364 9365“345678901” 9365 - 9374 “Browning R.” 9375 - 94059406 - 9409 9544“456789012” 9544 - 9553 “Eliot, T.S.” 9554 - 95849585 - 9588 9622 “567890123” 9622 - 9631 “Shelley, P.” 9632 - 96629663 - 9666 NULL

7 Page 7 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation NOTE: 1. We still need to allocate 45-contiguous bytes (the size of our record). 2.Records may be allocated contiguously (as with records 3 and 4) but need not be. If contiguous bytes are available, they will be allocated. If not, the next (available) grouping of 45-bytes will be allocated. 3.The space will be Allocated at RUN-TIME; NOT before Does that mean that we must request 45-bytes of contiguous storage 5 times (once for each record) ??? YES ! How do we request the needed memory ?? In C, the necessary statement is: newcust = (struct custrec *) malloc(sizeof(struct custrec)); Assuming that we had previously made the declaration: struct custrec * newcust;

8 Page 8 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Command Explanation: struct custrec * newcust; newcust Sets aside 4-bytes of storage at location newcust. newcust = (struct custrec *) malloc(sizeof(struct custrec)); Return the number of bytes we need (in our case: 45) newcust = (struct custrec *) malloc(sizeof(struct custrec)); mallocstdlib.h Function malloc (found in stdlib.h) takes one argument (the number of contiguous bytes of RAM we need) and returns the the address of where those contiguous bytes can be found.

9 Page 9 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation newcust = (struct custrec *) malloc(sizeof(struct custrec)); REMEMBER REMEMBER: When we refer to an address, we MUST know struct what type of data is stored there (in our case, the data type struct custrec custrec). We need to specify the data type which will be found at that address in advance newcust = (struct custrec *) malloc(sizeof(struct custrec)); Store the address at which 45-bytes of contiguous (where the data struct custrecnewcust type struct custrec can be found) at location (variable) newcust. What if there is NOT enough contiguous RAM available ??

10 Page 10 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation A good programmer will always check: newcust = (struct custrec *) malloc(sizeof(struct custrec)); if (newcust == NULL) // is there enough memory? { puts("Memory allocation failed - Bye!"); return(0); } return(0); } malloc Function malloc will return the value NULL if the requested amount of contiguous RAM could not be found What about the C code necessary ?? The code that follows assumes that: 1. The database will be entered from the keyboard 2. The User will continue entering records until they enter a RETURN for the customer’s name

11 Page 11 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation #include struct custrec { char ssn[10], name[31]; struct custrec * next; }; int main() { struct custrec *newcust, *first, *previous, *present; int numrecs = 0; do { newcust = (struct custrec *) malloc(sizeof(struct custrec)); if (newcust == NULL) { puts("memory allocation failed - bye!"); return 0; } if (numrecs == 0) first = newcust; else previous->next = newcust; newcust->next = NULL; numrecs++; printf("Enter Name for customer %d (Return to Quit): ",numrecs); gets(newcust->name); if (strlen(newcust->name)>0) { printf("Enter SSN for customer %s: ",newcust->name); gets(newcust->ssn); } previous = newcust; } while (strlen(newcust->name)>0); return 0; }

12 Page 12 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation There is one additional feature to dynamically allocated lists: We can free-up memory which we no longer require Assume we had set up our list (as before): “123456789” 9254 - 9263 “Chaucer, G.” 9264 - 9294 9295 - 9298 9322 “234567890” 9322- 9331 “Milton, J.” 9332 - 93609361- 9364 9365“345678901” 9365 - 9374 “Browning R.” 9375 - 94059406 - 9409 9544“456789012” 9544 - 9553 “Eliot, T.S.” 9554 - 95849585 - 9588 9622 “567890123” 9622 - 9631 “Shelley, P.” 9632 - 96629663 - 9666 NULL And we now wished to eliminate Eliot from the list

13 Page 13 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation How?? The procedure is relatively simple: newcust 1.Get the address of the record to be deleted (assume we store the address in location newcust). 2.Rearrange the pointers: a.If the first record, make the next record the first record b.If the last record, make the previous record the last one c.Otherwise, have the previous record point to the next record newcust 3.Free the address stored (at location newcust). The C command necessary ??? newcust Assuming we have already stored the address to be freed at location newcust, all we need to do is issue the command: free(newcust);

14 Page 14 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation How is this different than re-arranging the pointers as we did with simple linked lists ?? Unlike our previous examples, the space allocated for the record 95449588 (in our example, locations 9544 to 9588) are now available for reuse.

15 Page 15 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation

16 Page 16 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation

17 Page 17 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation

18 Page 18 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation

19 Page 19 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation

20 Page 20 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation


Download ppt "Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec."

Similar presentations


Ads by Google