Computing Reviews
Today's Issue Hot Topics Search Browse Recommended My Account Log In
Review Help
Search
Memory as a programming concept in C and C++
Franek F., Cambridge University Press, New York, NY, 2003. 250 pp. Type: Book (9780521520430)
Date Reviewed: Apr 30 2004

This book is aimed at intermediate-level C/C++ programmers. Neither a programming nor a language text, it attempts to fill the gap, perceived by the author, in the understanding that intermediate-level programmers have of how memory affects the behavior of C and C++ programs.

Many program errors and failures are the result of problems in using memory, such as references through deallocated pointers, or deallocation failures resulting in memory that is lost to the program (the dreaded memory leak). The book concentrates on providing a comprehensive view of memory in program behavior, in order to help programmers prevent such failures, and aid in detecting them. Topics covered include static allocation by the compiler and linker, automatic allocation on the stack at runtime, and explicit dynamic allocation and deallocation via malloc/free in C, and new/delete in C++.

There are 11 chapters in the book. The first is an introduction to the topics in the rest of the book. Chapter 2 covers compilation, linking, and related subjects. Chapters 3 through 5 review pointers and address concepts, dynamic memory allocation and deallocation, and functions and function calls. The discussions focus on memory issues.

Chapters 6 and 7 cover the memory representation of single and multi-dimensional arrays and related issues, such as passing arrays to functions and index checking. Chapter 8 covers the differences introduced by classes and class constructors and destructors in C++. Chapter 9 is a simple introduction to linked lists, useful mostly as an example of dynamic memory allocation. The material here on moving linked lists in memory and converting them to use relative addresses for storing on disk or passing across communication channels, though possibly of interest to some readers, diverges from the main subject of the book.

Chapter 10 groups memory leaks into seven types: orphan allocation, hidden allocation, undetermined ownership, insufficient destructor, incorrect assignment, exception-unsafe code, and external components. Examples of each are presented to explain how they occur and how they can be corrected. The bulk of the chapter demonstrates how to detect memory leaks in C using trace versions of the standard malloc family of memory management calls, and in C++ by overloading the new and delete operators. The complications of this approach in C++ are discussed, and object tracing, and the use of smart pointers, is illustrated. The final chapter discusses process and thread issues, including a brief description of Unix interprocess communication methods in general, and provides a more detailed explanation and example of Unix shared memory segments. This is used to show how threads can also lead to memory leaks.

Four appendices, a glossary of over 260 terms, and an index finish off the book. The first appendix, a solution to the Tower of Hanoi problem as an example of recursion, seems out of place. Two other appendices include code for debugging memory leaks in C++ by tracing memory and object use, with examples using the tracing code. This is useful code that could help both students and practitioners find memory leaks. The last appendix includes additional code for implementing a logging facility that is thread safe, also with an example. The code samples in the text and the appendices are available from an associated Web site. I downloaded this code, and found many of the programs did not compile. While most of the errors were trivial (C++ style comments in C files, missing include files, missing declarations, and typographical errors) and only annoying, such sloppiness leads one to doubt the programs’ correctness.

The book will be useful to C and C++ programmers who are beyond the beginner stage, but not yet experienced or really comfortable with how memory is used, especially dynamically. It serves its purpose of providing a supplemental text to introductory C or C++ books. Exercises are included at the end of each chapter. No answers are provided or currently available on the Web site, despite claims to the contrary in the book. The memory tracing routines could be helpful to anyone debugging memory problems who doesn’t already have either one of the available commercial products, or one of the debugging libraries available on the Web. Experienced debuggers of memory allocation problems may find that they already know both how to avoid the problems described, and how to debug them if they do occur. Since an ounce of preventing memory leaks is worth a pound of debugging, the book could be improved by suggesting methods to find leaks before testing and debugging, such as during code reviews.

Reviewer:  Andrew R. Huber Review #: CR129532 (0411-1307)
Bookmark and Share
  Featured Reviewer  
 
Memory Management (Garbage Collection) (D.3.4 ... )
 
 
C (D.3.2 ... )
 
 
C++ (D.3.2 ... )
 
 
General (D.1.0 )
 
 
General (D.3.0 )
 
 
Language Constructs and Features (D.3.3 )
 
  more  
Would you recommend this review?
yes
no
Other reviews under "Memory Management (Garbage Collection)": Date
Hardware support for real-time embedded multiprocessor system-on-a-chip memory management
Shalan M., Vincent J I.  Hardware/software codesign (Proceedings of the tenth international symposium, Estes Park, Colorado, May 6-8, 2002)79-84, 2002. Type: Proceedings
Jan 30 2004
Interprocedural pointer alias analysis
Hind M., Burke M., Carini P., Choi J. ACM Transactions on Programming Languages and Systems 21(4): 848-894, 1999. Type: Article
Mar 1 2000
Myths and realities: the performance impact of garbage collection
Blackburn S., Cheng P., McKinley K.  Measurement and modeling of computer systems (Proceedings of the joint international conference, New York, NY, USA, Jun 10-14, 2004)25-36, 2004. Type: Proceedings
Sep 7 2004
more...

E-Mail This Printer-Friendly
Send Your Comments
Contact Us
Reproduction in whole or in part without permission is prohibited.   Copyright 1999-2024 ThinkLoud®
Terms of Use
| Privacy Policy