Embedded Systems Design - July 2008 - (Page 10) programming pointers BY Dan Saks making it remarkably easy to recently presented arguments insert customized memory for and against using dynamic managers into existing code. memory allocation in C and 1 I do agree that You can even use allocation C++ programs. functions to place objects reptruly safety-critical systems resenting device registers at should avoid using dynamic almemory-mapped locations. location because the associated This month, I’ll contrast risks outweigh the advantages. the standard allocation and However, I strongly suspect that deallocation facilities in C with many other embedded systems those in C++. Understanding could be improved by judiciousthe differences between these ly using dynamic memory allofacilities is insightful regardless cation. In places where the stanof which language you use. dard C or C++ allocation and deallocation functions are less THE STANDARD C than ideal, a customized memoFUNCTIONS ry manager might work much Standard C provides two membetter. ory allocation functions, malIdeally, a customized memory manager should look and loc and calloc, and one dealact like a standard one to the greatlocation function, free. A fourth C and C++ have strikingly function, realloc, does both dealest extent possible. Although the different approaches to customized allocation policy will location and allocation. All four be different from the policy of a functions are declared in the stanmanaging dynamic standard one (that’s the whole dard header . memory. point of using a custom scheme), The storage for an object allothe parameters and return values cated using an allocation function of the customized allocation and deallocation functions (malloc, calloc, or realloc) remains allocated until the should be as similar as possible to the standard ones. This program passes the object’s address as an argument to a similarity lets you build and test your code using standard deallocation function (free or realloc). Attempting to memory management functions and then slip customized deallocate storage that isn’t currently allocated produces functions in or out as needed with minimal fuss. undefined behavior. (I explained the distinctions among Understanding the ins and outs of dynamic memory undefined, unspecified, and implementation-defined bemanagement is especially worthwhile for C++ programhavior in earlier columns.)1, 2 mers. C++ offers many facilities—most notably classes The standard declares malloc as: with constructors and destructors—that dramatically diminish the incidence of memory leaks. C++ also lets you void *malloc(size_t size); define allocation and deallocation functions for each class, Calling malloc(s) allocates storage for an object whose size is s bytes. If the allocation succeeds, the call returns a Dan Saks is president of Saks & Associates, pointer to the allocated storage. If it fails, the call returns a a C/C++ training and consulting company. For more information about Dan Saks, visit his null pointer. website at www.dansaks.com. Dan also welParameter size has type size_t. As I explained in a comes your feedback: e-mail him at column last year, size_t is a typedef declared in dsaks@wittenberg.edu. as well as several other headers. The type is 10 JULY 2008 | embedded systems design | www.embedded.com Dynamic allocation in C and C++ I http://www.dansaks.com http://www.embedded.com
Table of Contents Feed for the Digital Edition of Embedded Systems Design - July 2008 Embedded Systems Design - July 2008 Contents #Include Parity Bit Programming Pointers Interactive C-code Cleaning Tool Supports Multiprocessor SoC Design Building a Power Supply for Discontinuous Transmission Wireless Networks An Exception Primer Advertising Index Break Points Marketplace Embedded Systems Design - July 2008 Embedded Systems Design - July 2008 - Embedded Systems Design - July 2008 (Page Cover1) Embedded Systems Design - July 2008 - Embedded Systems Design - July 2008 (Page Cover2) Embedded Systems Design - July 2008 - Embedded Systems Design - July 2008 (Page 1) Embedded Systems Design - July 2008 - Embedded Systems Design - July 2008 (Page 2) Embedded Systems Design - July 2008 - Contents (Page 3) Embedded Systems Design - July 2008 - Contents (Page 4) Embedded Systems Design - July 2008 - Contents (Page 5) Embedded Systems Design - July 2008 - Contents (Page 6) Embedded Systems Design - July 2008 - #Include (Page 7) Embedded Systems Design - July 2008 - #Include (Page 8) Embedded Systems Design - July 2008 - Parity Bit (Page 9) Embedded Systems Design - July 2008 - Programming Pointers (Page 10) Embedded Systems Design - July 2008 - Programming Pointers (Page 11) Embedded Systems Design - July 2008 - Programming Pointers (Page 12) Embedded Systems Design - July 2008 - Programming Pointers (Page 13) Embedded Systems Design - July 2008 - Programming Pointers (Page 14) Embedded Systems Design - July 2008 - Programming Pointers (Page 15) Embedded Systems Design - July 2008 - Programming Pointers (Page 16) Embedded Systems Design - July 2008 - Programming Pointers (Page 17) Embedded Systems Design - July 2008 - Interactive C-code Cleaning Tool Supports Multiprocessor SoC Design (Page 18) Embedded Systems Design - July 2008 - Interactive C-code Cleaning Tool Supports Multiprocessor SoC Design (Page 19) Embedded Systems Design - July 2008 - Interactive C-code Cleaning Tool Supports Multiprocessor SoC Design (Page 20) Embedded Systems Design - July 2008 - Interactive C-code Cleaning Tool Supports Multiprocessor SoC Design (Page 21) Embedded Systems Design - July 2008 - Interactive C-code Cleaning Tool Supports Multiprocessor SoC Design (Page 22) Embedded Systems Design - July 2008 - Interactive C-code Cleaning Tool Supports Multiprocessor SoC Design (Page 23) Embedded Systems Design - July 2008 - Interactive C-code Cleaning Tool Supports Multiprocessor SoC Design (Page 24) Embedded Systems Design - July 2008 - Interactive C-code Cleaning Tool Supports Multiprocessor SoC Design (Page 25) Embedded Systems Design - July 2008 - Interactive C-code Cleaning Tool Supports Multiprocessor SoC Design (Page 26) Embedded Systems Design - July 2008 - Building a Power Supply for Discontinuous Transmission Wireless Networks (Page 27) Embedded Systems Design - July 2008 - Building a Power Supply for Discontinuous Transmission Wireless Networks (Page 28) Embedded Systems Design - July 2008 - Building a Power Supply for Discontinuous Transmission Wireless Networks (Page 29) Embedded Systems Design - July 2008 - Building a Power Supply for Discontinuous Transmission Wireless Networks (Page 30) Embedded Systems Design - July 2008 - Building a Power Supply for Discontinuous Transmission Wireless Networks (Page 31) Embedded Systems Design - July 2008 - Building a Power Supply for Discontinuous Transmission Wireless Networks (Page 32) Embedded Systems Design - July 2008 - Building a Power Supply for Discontinuous Transmission Wireless Networks (Page 33) Embedded Systems Design - July 2008 - Building a Power Supply for Discontinuous Transmission Wireless Networks (Page 34) Embedded Systems Design - July 2008 - An Exception Primer (Page 35) Embedded Systems Design - July 2008 - An Exception Primer (Page 36) Embedded Systems Design - July 2008 - An Exception Primer (Page 37) Embedded Systems Design - July 2008 - An Exception Primer (Page 38) Embedded Systems Design - July 2008 - An Exception Primer (Page 39) Embedded Systems Design - July 2008 - An Exception Primer (Page 40) Embedded Systems Design - July 2008 - An Exception Primer (Page 41) Embedded Systems Design - July 2008 - An Exception Primer (Page 42) Embedded Systems Design - July 2008 - An Exception Primer (Page 43) Embedded Systems Design - July 2008 - Advertising Index (Page 44) Embedded Systems Design - July 2008 - Break Points (Page 45) Embedded Systems Design - July 2008 - Break Points (Page 46) Embedded Systems Design - July 2008 - Marketplace (Page 47) Embedded Systems Design - July 2008 - Marketplace (Page 48) Embedded Systems Design - July 2008 - Marketplace (Page Cover3) Embedded Systems Design - July 2008 - Marketplace (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.