Embedded Systems Design - July 2008 - (Page 12) programming pointers locates the “old” object whose an alias for some unsigned malloc and calloc always address is p and returns a integer type, typically unsigned int or unsigned interpret their size arguments as a pointer to a “new” object with size s. The contents of the new long, or possibly even unnon-negative values. However, object will be the same as what signed long long.3 Each they might be zero. was in the old object prior to Standard C implementation the call, up to the lesser of the is supposed to choose the old object’s size and the new unsigned integer that’s big object’s size. Any bytes in the new object beyond the size of enough—but no bigger than needed—to represent the the old object will have indeterminate values. size of the largest possible object on the target platform. As with the other allocation functions, a call to realloc The argument in a typical call to malloc is a sizeof expression. (A sizeof expression yields a value of type returns a pointer to the allocated storage if the allocation succeeds; otherwise, it returns a null pointer without dealsize_t.) For example, if p is a pointer to a widget, then: locating the old object. Inasmuch as size_t is always an unsigned type, malp = malloc(sizeof(widget)); loc and calloc always interpret their size arguments as a non-negative values. However, they might be zero. For exassigns p the address of a dynamically allocated widget. ample, if the n in: Or: p = malloc(10 * sizeof(widget)); p = malloc(n * sizeof(widget)); assigns p the address of the first element in a dynamically allocated array of 10 widgets. The calloc function is an alternative to malloc declared in the standard as: void *calloc(size_t nmemb, size_t size); is zero, the call attempts to allocate a zero-sized array. Whether the call returns a null or non-null pointer is implementation-defined. In either event, dereferencing that pointer would yield undefined behavior. The standard declares free as: void free(void *ptr); Calling calloc(n, s) allocates storage for an array of n objects, each of which is s bytes in size. As with malloc, a call to calloc returns a pointer to the allocated storage if the allocation succeeds and returns a null pointer otherwise. Thus, to allocate an array of 10 widgets, you can call either: p = malloc(10 * sizeof(widget)); If p is a null pointer, calling free(p) does nothing. Otherwise, the call deallocates the storage for the object addressed by p. NEW- AND DELETE- EXPRESSIONS IN C++ The Standard C allocation and deallocation functions are also available in C++, but C code using the allocation functions doesn’t always compile in C++. For example, if p is a pointer to a widget, the conventional C usage: p = malloc(sizeof(widget)); or: p = calloc(10, sizeof(widget)); The only substantive difference between these expressions is the value of the allocated storage. After calling malloc, the value in the allocated storage is indeterminate, which is the standard’s way of saying the storage contains whatever residual values were left there by the previous occupant. After calling calloc, all bits in the allocated storage are set to zero. The realloc function reallocates an object as an object of a possibly different size. The standard declares it as: void *realloc(void *ptr, size_t size); provokes a compile-time diagnostic (either a warning or an error message) when compiled as C++. The complaint is that the expression attempts an invalid conversion from void * to widget *. As I explained in my most recent online-only column, the conversion is valid in C, but not in C++.4 If you want the allocation to compile in C++, you must use a cast, as in: p = (widget *)malloc(sizeof(widget)); Using a “new-style” cast is arguably better:5 p = static_cast (malloc(sizeof(widget))); If p is a null pointer, then calling realloc(p, s) yields the same result as calling malloc(s). Otherwise, the call deal- 12 JULY 2008 | embedded systems design | www.embedded.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.