Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science: A Structured Programming Approach Using C1 10-4 Memory Allocation Functions C gives us two choices when we want to reserve memory locations.

Similar presentations


Presentation on theme: "Computer Science: A Structured Programming Approach Using C1 10-4 Memory Allocation Functions C gives us two choices when we want to reserve memory locations."— Presentation transcript:

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

2 Computer Science: A Structured Programming Approach Using C2 FIGURE Memory Allocation

3 Computer Science: A Structured Programming Approach Using C3 FIGURE A Conceptual View of Memory

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

5 Computer Science: A Structured Programming Approach Using C5 FIGURE Accessing Dynamic Memory

6 Computer Science: A Structured Programming Approach Using C6 FIGURE Memory Management Functions

7 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

8 Computer Science: A Structured Programming Approach Using C8 FIGURE malloc

9 Computer Science: A Structured Programming Approach Using C9 FIGURE calloc

10 Computer Science: A Structured Programming Approach Using C10 FIGURE realloc

11 Computer Science: A Structured Programming Approach Using C11 FIGURE Freeing Memory

12 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

13 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

14 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.

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

16 Computer Science: A Structured Programming Approach Using C16 FIGURE A Ragged Array

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

18 Computer Science: A Structured Programming Approach Using C18 FIGURE Selection Sort with Pointers—Structure Chart

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

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

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

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

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

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

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

26 Computer Science: A Structured Programming Approach Using C26 FIGURE Dynamic Array Structure Chart

27 Computer Science: A Structured Programming Approach Using C27 FIGURE Ragged Array Structure

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


Download ppt "Computer Science: A Structured Programming Approach Using C1 10-4 Memory Allocation Functions C gives us two choices when we want to reserve memory locations."

Similar presentations


Ads by Google