Arrays and pointers are low-level building blocks of C programs that provide fast access to hardware memory. This chapter discusses the different ways to organize and access memory.
22.1 |
Pointer Pathology |
504 |
22.2 |
Further Pointer Pathology with Heap Memory |
506 |
22.3 |
Memory Access Summary |
509 |
22.4 |
Introduction to Arrays |
509 |
22.5 |
Pointer Arithmetic |
510 |
22.6 |
Arrays, Functions, and Return Values |
511 |
22.7 |
Different Kinds of Arrays |
513 |
22.8 |
Valid Pointer Operations |
513 |
22.9 |
What Happens If new Fails? |
515 |
22.10 |
Chapter Summary |
519 |
Direct manipulation of memory entails some serious risks and requires good practices and thorough testing to avoid serious errors. Improper use of pointers and dynamic memory can cause program crashes that result from heap corruption and memory leaks. Heap corruption is especially difficult to debug because it generally leads to segmentation faults that halt the program at a point in the code that may be far from the point at which the heap became corrupted.
Both Qt and Standard Library container classes permit the safe use of dynamic memory without adversely affecting performance.[1] Arrays are used to implement most container classes but are hidden from client code. The safety factors come from the careful design of each container API so that actions that might produce memory problems are not permitted.
[1] In the sequel, whenever we use the term container, with no further qualification, we mean Qt or Standard Library container.
Qt offers many containers, ranging from high-level template classes such as the ones we discussed in Section 10.2 to low-level containers such as QBitArray and QByteArray.
Generally, when writing applications that reuse those containers, it is easy to avoid the use of arrays entirely. When Qt is not available, or when you are writing an interface to C code, you may need to use arrays and pointers and work directly with allocated memory.
Part I: Introduction to C++ and Qt 4
C++ Introduction
Classes
Introduction to Qt
Lists
Functions
Inheritance and Polymorphism
Part II: Higher-Level Programming
Libraries
Introduction to Design Patterns
QObject
Generics and Containers
Qt GUI Widgets
Concurrency
Validation and Regular Expressions
Parsing XML
Meta Objects, Properties, and Reflective Programming
More Design Patterns
Models and Views
Qt SQL Classes
Part III: C++ Language Reference
Types and Expressions
Scope and Storage Class
Statements and Control Structures
Memory Access
Chapter Summary
Inheritance in Detail
Miscellaneous Topics
Part IV: Programming Assignments
MP3 Jukebox Assignments
Part V: Appendices
MP3 Jukebox Assignments
Bibliography
MP3 Jukebox Assignments