Memory Allocation Functions C gives us two choices

  • Slides: 28
Download presentation
Memory Allocation Functions C gives us two choices when we want to reserve memory

Memory Allocation Functions C gives us two choices when we want to reserve memory locations for an object: static allocation and dynamic allocation. Computer Science: A Structured Programming Approach Using C 1

Memory Allocation Computer Science: A Structured Programming Approach Using C 2

Memory Allocation Computer Science: A Structured Programming Approach Using C 2

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

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

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

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

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

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

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

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

Note Memory Allocation Casting Prior to C 99, it was necessary to cast the

Note Memory Allocation Casting Prior to C 99, 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 7

malloc. Computer Science: A Structured Programming Approach Using C 8

malloc. Computer Science: A Structured Programming Approach Using C 8

calloc Computer Science: A Structured Programming Approach Using C 9

calloc Computer Science: A Structured Programming Approach Using C 9

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

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

Freeing Memory Computer Science: A Structured Programming Approach Using C 11

Freeing Memory Computer Science: A Structured Programming Approach Using C 11

Note Using a pointer after its memory has been released is a common programming

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 12

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

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 13

Array of Pointers Another useful structure that uses arrays and pointers is an array

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 14

A Ragged Table Computer Science: A Structured Programming Approach Using C 15

A Ragged Table Computer Science: A Structured Programming Approach Using C 15

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

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

Selection Sort with Pointers—Structure Computer Science: A Structured Programming Approach Using. Chart C 17

Selection Sort with Pointers—Structure Computer Science: A Structured Programming Approach Using. Chart C 17

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 18

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 18

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 19

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 19

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 20

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 20

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 21

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 21

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 22

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 22

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 23

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 23

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 24

Selection Sort Revisited Computer Science: A Structured Programming Approach Using C 24

Software Engineering Pointer applications need careful design to ensure that they work correctly and

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. Computer Science: A Structured Programming Approach Using C 25

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

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

Testing Memory Reuse Computer Science: A Structured Programming Approach Using C 27

Testing Memory Reuse Computer Science: A Structured Programming Approach Using C 27

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

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