45
Computer Science: A Structured Programming Approach Using C 1 0-4 Memory Allocation Functio C gives us two choices when we want to reserve C gives us two choices when we want to reserve memory locations for an object: static memory locations for an object: static allocation and dynamic allocation. allocation and dynamic allocation. Memory Usage Static Memory Allocation Dynamic Memory Allocation Memory Allocation Functions Releasing Memory (free) Topics discussed in this section: Topics discussed in this section:

10-4 Memory Allocation Functions

  • Upload
    beate

  • View
    62

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 1

10-4 Memory Allocation Functions

C gives us two choices when we want to reserve C gives us two choices when we want to reserve memory locations for an object: static allocation and memory locations for an object: static allocation and dynamic allocation. dynamic allocation.

Memory UsageStatic Memory AllocationDynamic Memory AllocationMemory Allocation FunctionsReleasing Memory (free)

Topics discussed in this section:Topics discussed in this section:

Page 2: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 2

FIGURE 10-11 Memory Allocation

Page 3: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 3

FIGURE 10-12 A Conceptual View of Memory

Page 4: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 4

We can refer to memory allocated in the heap only through a pointer.

NoteNote

Page 5: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 5

FIGURE 10-13 Accessing Dynamic Memory

Page 6: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 6

FIGURE 10-14 Memory Management Functions

Page 7: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 7

Memory Allocation CastingPrior 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)

NoteNote

Page 8: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 8

FIGURE 10-15 malloc

Page 9: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 9

FIGURE 10-16 calloc

Page 10: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 10

FIGURE 10-17 realloc

Page 11: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 11

FIGURE 10-18 Freeing Memory

Page 12: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 12

Using a pointer after its memory has been released is a common programming error. Guard against it

by clearing the pointer.

NoteNote

Page 13: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 13

The pointer used to free memory must be of the same type as the pointer used to allocate the memory.

NoteNote

Page 14: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 14

10-5 Array of Pointers

Another useful structure that uses arrays and pointers Another useful structure that uses arrays and pointers is an array of pointers. This structure is especially is an array of pointers. This structure is especially helpful when the number of elements in the array is helpful when the number of elements in the array is variable.variable.

Page 15: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 15

Table 10-2 A Ragged Table

Page 16: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 16

FIGURE 10-19 A Ragged Array

Page 17: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 17

10-6 Programming Applications

This section contains two applications. The first is a This section contains two applications. The first is a rewrite of the selection sort, this time using pointers. rewrite of the selection sort, this time using pointers. The second uses dynamic arrays.The second uses dynamic arrays.

Selection Sort RevisitedDynamic Array

Topics discussed in this section:Topics discussed in this section:

Page 18: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 18

FIGURE 10-20 Selection Sort with Pointers—Structure Chart

Page 19: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 19

PROGRAM 10-4 Selection Sort Revisited

Page 20: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 20

PROGRAM 10-4 Selection Sort Revisited

Page 21: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 21

PROGRAM 10-4 Selection Sort Revisited

Page 22: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 22

PROGRAM 10-4 Selection Sort Revisited

Page 23: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 10-4 Selection Sort Revisited

Page 24: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 24

PROGRAM 10-4 Selection Sort Revisited

Page 25: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 25

PROGRAM 10-4 Selection Sort Revisited

Page 26: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 26

FIGURE 10-21 Dynamic Array Structure Chart

Page 27: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 27

FIGURE 10-22 Ragged Array Structure

Page 28: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 28

PROGRAM 10-5 Dynamic Arrays: main

Page 29: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 29

PROGRAM 10-5 Dynamic Arrays: main

Page 30: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 30

PROGRAM 10-6 Dynamic Arrays: buildTable

Page 31: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 31

PROGRAM 10-5 Dynamic Arrays: buildTable

Page 32: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 10-7 Dynamic Arrays: fillTable

Page 33: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 33

PROGRAM 10-7 Dynamic Arrays: fillTable

Page 34: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 34

PROGRAM 10-8 Dynamic Arrays: Process Table

Page 35: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 35

PROGRAM 10-8 Dynamic Arrays: Process Table

Page 36: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 36

PROGRAM 10-9 Dynamic Arrays: Find Row Minimum

Page 37: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 37

PROGRAM 10-9 Dynamic Arrays: Find Row Minimum

Page 38: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 38

PROGRAM 10-10 Dynamic Arrays: Find Row Maximum

Page 39: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 39

PROGRAM 10-11 Dynamic Arrays: Find Row Average

Page 40: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 40

PROGRAM 10-12 Dynamic Arrays: Find Smaller

Page 41: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 41

PROGRAM 10-13 Dynamic Arrays: Find Larger

Page 42: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 42

10-7 Software Engineering

Pointer applications need careful design to ensure that Pointer applications need careful design to ensure that they work correctly and efficiently. The programmer they work correctly and efficiently. The programmer not only must take great care in the program design not only must take great care in the program design but also must carefully consider the data structures but also must carefully consider the data structures that are inherent with pointer applications. that are inherent with pointer applications.

Pointers and Function CallsPointers and ArraysArray Index CommutativityDynamic Memory: Theory versus Practice

Topics discussed in this section:Topics discussed in this section:

Page 43: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 43

Whenever possible, use value parameters.

NoteNote

Page 44: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 44

PROGRAM 10-14 Testing Memory Reuse

Page 45: 10-4   Memory Allocation Functions

Computer Science: A Structured Programming Approach Using C 45

PROGRAM 10-14 Testing Memory Reuse