10-4 memory allocation functions

Post on 25-Feb-2016

69 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

TRANSCRIPT

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:

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

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

NoteNote

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

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

Computer Science: A Structured Programming Approach Using C 8

FIGURE 10-15 malloc

Computer Science: A Structured Programming Approach Using C 9

FIGURE 10-16 calloc

Computer Science: A Structured Programming Approach Using C 10

FIGURE 10-17 realloc

Computer Science: A Structured Programming Approach Using C 11

FIGURE 10-18 Freeing Memory

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

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

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.

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 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:

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 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:

Computer Science: A Structured Programming Approach Using C 43

Whenever possible, use value parameters.

NoteNote

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

top related