Embedded Systems Design - July 2008 - (Page 16) programming pointers • In C++, it’s the programmer’s responsibility to choose the correct form for each delete-expression (for a single object or array) by including or omitting []. In C, you simply call free(p) whether p points to a single object or to an array. structor to that storage to produce a properly constructed widget object. C and C++ handle dynamic allocation in a fundamentally different way: Whereas malloc just allocates storage of indeterminate value, a new-expression can create a properly initialized object. What about using calloc instead of malloc? Doesn’t calloc initialize objects to zero? Indeed, for some types, setting all bits to zero is a reasonable and useful initial value, but for many class types, it isn’t. In fact, as the C standard notes, all bits zero might not even be the proper representation for a floating-point zero or a • If C++ really did place a greater premium on type safety than C does, then wouldn’t C++ simply avoid these errors by proAlthough C++ places great viding just one form for emphasis on type safety, in this delete-expressions? case, it trades a little type safety Although C++ places great emphasis on type safefor potentially significant ty, it’s not to the exclusion of performance improvements in other concerns, such as permemory allocation. formance. In this case, C++ trades a little type safety for potentially significant performance improvements in null pointer. memory allocation. As I’ll explain in an upcoming article, C++ classes use special member functions called dethis design for new- and delete-expressions makes it easistructors to provide automatic resource deallocation. A deer to implement and use efficient customized memory structor’s name is the same as the constructor’s name, but managers. with a ~ in front of the name, as in: INITIALIZING ALLOCATED OBJECTS In C++, a class constructor is a special class member function that initializes objects of its class type. A constructor’s function name is always the same as its class name, as in: class widget { public: widget(); }; class widget { public: widget(); ~widget(); }; // a constructor // a destructor // a constructor Constructors provide guaranteed initialization for class objects. You don’t write calls to constructors—the compiler generates them for you. Whenever you define an object with a class type, the compiler automatically plants a call to the object’s constructor at the right place in the program. For guaranteed initialization to really be guaranteed, the compiler must generate a call to the appropriate constructor wherever the source code creates an object, including in new-expressions. Thus, for a class type such as widget, a new-expression such as in: p = new widget; Just as new-expressions invoke constructors, delete-expressions invoke destructors. I will elaborate the connection between new-expressions and constructors, and between delete-expressions and destructors, in a forthcoming column. ■ ENDNOTES: 1. Saks, Dan.“The yin and yang of dynamic allocation,” Embedded Systems Design, May, 2008, p. 12. Available online at www.embedded.com/207402546. Saks, Dan.“As Precise as Possible,” Embedded Systems Programming, April, 2002, p. 43. Available online at www.embedded.com/9900563. Saks, Dan.“Why size_t matters,” Embedded Systems Design, July, 2007, p. 13. Available online at www.embedded.com/200900195. Saks, Dan.“Into, but not out of, the void,” Embedded.com, June 2008, www.embedded.com/208403407. Saks, Dan.“Cast with caution,” Embedded Systems Design, July, 2006, p. 15. Available online at www.embedded.com/191600535. 2. 3. 4. 5. doesn’t just allocate storage for a widget; it applies a con- 16 JULY 2008 | embedded systems design | www.embedded.com http://www.embedded.com/207402546 http://www.embedded.com/9900563 http://www.embedded.com/200900195 http://www.embedded.com/208403407 http://www.embedded.com/191600535 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.