Computer Science: A Structured Programming Approach Using C Memory Allocation Functions C gives us two choices when we want to reserve memory locations for an object: static allocation and dynamic allocation. Memory Usage Static Memory Allocation Dynamic Memory Allocation Memory Allocation Functions Releasing Memory (free) Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C2 FIGURE Memory Allocation
Computer Science: A Structured Programming Approach Using C3 FIGURE A Conceptual View of Memory
Computer Science: A Structured Programming Approach Using C4 We can refer to memory allocated in the heap only through a pointer. Note
Computer Science: A Structured Programming Approach Using C5 FIGURE Accessing Dynamic Memory
Computer Science: A Structured Programming Approach Using C6 FIGURE Memory Management Functions
Computer Science: A Structured Programming Approach Using C7 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) Note
Computer Science: A Structured Programming Approach Using C8 FIGURE malloc
Computer Science: A Structured Programming Approach Using C9 FIGURE calloc
Computer Science: A Structured Programming Approach Using C10 FIGURE realloc
Computer Science: A Structured Programming Approach Using C11 FIGURE Freeing Memory
Computer Science: A Structured Programming Approach Using C12 Using a pointer after its memory has been released is a common programming error. Guard against it by clearing the pointer. Note
Computer Science: A Structured Programming Approach Using C13 The pointer used to free memory must be of the same type as the pointer used to allocate the memory. Note
Computer Science: A Structured Programming Approach Using C 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 C15 Table 10-2A Ragged Table
Computer Science: A Structured Programming Approach Using C16 FIGURE A Ragged Array
Computer Science: A Structured Programming Approach Using C 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. Selection Sort Revisited Dynamic Array Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C18 FIGURE Selection Sort with Pointers—Structure Chart
Computer Science: A Structured Programming Approach Using C19 PROGRAM 10-4Selection Sort Revisited
Computer Science: A Structured Programming Approach Using C20 PROGRAM 10-4Selection Sort Revisited
Computer Science: A Structured Programming Approach Using C21 PROGRAM 10-4Selection Sort Revisited
Computer Science: A Structured Programming Approach Using C22 PROGRAM 10-4Selection Sort Revisited
Computer Science: A Structured Programming Approach Using C23 PROGRAM 10-4Selection Sort Revisited
Computer Science: A Structured Programming Approach Using C24 PROGRAM 10-4Selection Sort Revisited
Computer Science: A Structured Programming Approach Using C25 PROGRAM 10-4Selection Sort Revisited
Computer Science: A Structured Programming Approach Using C26 FIGURE Dynamic Array Structure Chart
Computer Science: A Structured Programming Approach Using C27 FIGURE Ragged Array Structure
Computer Science: A Structured Programming Approach Using C28 PROGRAM 10-5Dynamic Arrays: main
Computer Science: A Structured Programming Approach Using C29 PROGRAM 10-5Dynamic Arrays: main
Computer Science: A Structured Programming Approach Using C30 PROGRAM 10-6Dynamic Arrays: buildTable
Computer Science: A Structured Programming Approach Using C31 PROGRAM 10-5Dynamic Arrays: buildTable
Computer Science: A Structured Programming Approach Using C32 PROGRAM 10-7Dynamic Arrays: fillTable
Computer Science: A Structured Programming Approach Using C33 PROGRAM 10-7Dynamic Arrays: fillTable
Computer Science: A Structured Programming Approach Using C34 PROGRAM 10-8Dynamic Arrays: Process Table
Computer Science: A Structured Programming Approach Using C35 PROGRAM 10-8Dynamic Arrays: Process Table
Computer Science: A Structured Programming Approach Using C36 PROGRAM 10-9Dynamic Arrays: Find Row Minimum
Computer Science: A Structured Programming Approach Using C37 PROGRAM 10-9Dynamic Arrays: Find Row Minimum
Computer Science: A Structured Programming Approach Using C38 PROGRAM 10-10Dynamic Arrays: Find Row Maximum
Computer Science: A Structured Programming Approach Using C39 PROGRAM 10-11Dynamic Arrays: Find Row Average
Computer Science: A Structured Programming Approach Using C40 PROGRAM 10-12Dynamic Arrays: Find Smaller
Computer Science: A Structured Programming Approach Using C41 PROGRAM 10-13Dynamic Arrays: Find Larger
Computer Science: A Structured Programming Approach Using C 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. Pointers and Function Calls Pointers and Arrays Array Index Commutativity Dynamic Memory: Theory versus Practice Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C43 Whenever possible, use value parameters. Note
Computer Science: A Structured Programming Approach Using C44 PROGRAM 10-14Testing Memory Reuse
Computer Science: A Structured Programming Approach Using C45 PROGRAM 10-14Testing Memory Reuse