1 / 45

10-4 Memory Allocation Functions

10-4 Memory Allocation Functions. C gives us two choices when we want to reserve memory locations for an object: static allocation and dynamic allocation. . Topics discussed in this section:. Memory Usage Static Memory Allocation Dynamic Memory Allocation Memory Allocation Functions

beate
Télécharger la présentation

10-4 Memory Allocation Functions

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. 10-4 Memory Allocation Functions C gives us two choices when we want to reserve memory locations for an object: static allocation and dynamic allocation. Topics discussed in this section: Memory Usage Static Memory Allocation Dynamic Memory Allocation Memory Allocation Functions Releasing Memory (free) Computer Science: A Structured Programming Approach Using C

  2. FIGURE 10-11 Memory Allocation Computer Science: A Structured Programming Approach Using C

  3. FIGURE 10-12 A Conceptual View of Memory Computer Science: A Structured Programming Approach Using C

  4. Note We can refer to memory allocated in the heap only through a pointer. Computer Science: A Structured Programming Approach Using C

  5. FIGURE 10-13 Accessing Dynamic Memory Computer Science: A Structured Programming Approach Using C

  6. FIGURE 10-14 Memory Management Functions Computer Science: A Structured Programming Approach Using C

  7. Note Memory Allocation Casting Prior to C99, it was necessary to cast the pointer returned from a memory allocation function. While it is no longer necessary, it does no harm as long as the cast is correct. If you should be working with an earlier standard, the casting format is: pointer = (type*) malloc(size) Computer Science: A Structured Programming Approach Using C

  8. FIGURE 10-15malloc Computer Science: A Structured Programming Approach Using C

  9. FIGURE 10-16calloc Computer Science: A Structured Programming Approach Using C

  10. FIGURE 10-17realloc Computer Science: A Structured Programming Approach Using C

  11. FIGURE 10-18 Freeing Memory Computer Science: A Structured Programming Approach Using C

  12. Note Using a pointer after its memory has been released is a common programming error. Guard against it by clearing the pointer. Computer Science: A Structured Programming Approach Using C

  13. Note The pointer used to free memory must be of the same type as the pointer used to allocate the memory. Computer Science: A Structured Programming Approach Using C

  14. 10-5 Array of Pointers Another useful structure that uses arrays and pointers is an array of pointers. This structure is especially helpful when the number of elements in the array is variable. Computer Science: A Structured Programming Approach Using C

  15. Table 10-2 A Ragged Table Computer Science: A Structured Programming Approach Using C

  16. FIGURE 10-19 A Ragged Array Computer Science: A Structured Programming Approach Using C

  17. 10-6 Programming Applications This section contains two applications. The first is a rewrite of the selection sort, this time using pointers. The second uses dynamic arrays. Topics discussed in this section: Selection Sort Revisited Dynamic Array Computer Science: A Structured Programming Approach Using C

  18. FIGURE 10-20 Selection Sort with Pointers—Structure Chart Computer Science: A Structured Programming Approach Using C

  19. PROGRAM 10-4 Selection Sort Revisited Computer Science: A Structured Programming Approach Using C

  20. PROGRAM 10-4 Selection Sort Revisited Computer Science: A Structured Programming Approach Using C

  21. PROGRAM 10-4 Selection Sort Revisited Computer Science: A Structured Programming Approach Using C

  22. PROGRAM 10-4 Selection Sort Revisited Computer Science: A Structured Programming Approach Using C

  23. PROGRAM 10-4 Selection Sort Revisited Computer Science: A Structured Programming Approach Using C

  24. PROGRAM 10-4 Selection Sort Revisited Computer Science: A Structured Programming Approach Using C

  25. PROGRAM 10-4 Selection Sort Revisited Computer Science: A Structured Programming Approach Using C

  26. FIGURE 10-21 Dynamic Array Structure Chart Computer Science: A Structured Programming Approach Using C

  27. FIGURE 10-22 Ragged Array Structure Computer Science: A Structured Programming Approach Using C

  28. PROGRAM 10-5 Dynamic Arrays: main Computer Science: A Structured Programming Approach Using C

  29. PROGRAM 10-5 Dynamic Arrays: main Computer Science: A Structured Programming Approach Using C

  30. PROGRAM 10-6 Dynamic Arrays: buildTable Computer Science: A Structured Programming Approach Using C

  31. PROGRAM 10-5 Dynamic Arrays: buildTable Computer Science: A Structured Programming Approach Using C

  32. PROGRAM 10-7 Dynamic Arrays: fillTable Computer Science: A Structured Programming Approach Using C

  33. PROGRAM 10-7 Dynamic Arrays: fillTable Computer Science: A Structured Programming Approach Using C

  34. PROGRAM 10-8 Dynamic Arrays: Process Table Computer Science: A Structured Programming Approach Using C

  35. PROGRAM 10-8 Dynamic Arrays: Process Table Computer Science: A Structured Programming Approach Using C

  36. PROGRAM 10-9 Dynamic Arrays: Find Row Minimum Computer Science: A Structured Programming Approach Using C

  37. PROGRAM 10-9 Dynamic Arrays: Find Row Minimum Computer Science: A Structured Programming Approach Using C

  38. PROGRAM 10-10 Dynamic Arrays: Find Row Maximum Computer Science: A Structured Programming Approach Using C

  39. PROGRAM 10-11 Dynamic Arrays: Find Row Average Computer Science: A Structured Programming Approach Using C

  40. PROGRAM 10-12 Dynamic Arrays: Find Smaller Computer Science: A Structured Programming Approach Using C

  41. PROGRAM 10-13 Dynamic Arrays: Find Larger Computer Science: A Structured Programming Approach Using C

  42. 10-7 Software Engineering Pointer applications need careful design to ensure that they work correctly and efficiently. The programmer not only must take great care in the program design but also must carefully consider the data structures that are inherent with pointer applications. Topics discussed in this section: Pointers and Function Calls Pointers and Arrays Array Index Commutativity Dynamic Memory: Theory versus Practice Computer Science: A Structured Programming Approach Using C

  43. Note Whenever possible, use value parameters. Computer Science: A Structured Programming Approach Using C

  44. PROGRAM 10-14 Testing Memory Reuse Computer Science: A Structured Programming Approach Using C

  45. PROGRAM 10-14 Testing Memory Reuse Computer Science: A Structured Programming Approach Using C

More Related