Embedded Systems Design - June 2008 - (Page 16) programmer’s toolbox store size or shape information anywhere in the executable software. Look at the two arrays with any debugger, and you’ll see the exact same block of nine consecutive doubles. What’s more, we’ve all read the introductory C textbooks where we were told that any reference to an array is equivalent to a pointer to its first element. Thus: a b ptr_a = &a[0]; double b[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; I tend to use this kind of construct a lot, but strictly speaking, it’s not good practice. I’ve declared the array to be doubly dimensioned, but used a singly dimensioned initializer. That it works at all is yet another indication of the on-again, off-again equivalence of singly and doubly dimensioned arrays. To be rigorously correct, I should have written: double b[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; and: ptr_b = &b[0][0]; should all give the same result: a simple pointer to a double. Indeed, all through my code for class matrix, you’ll see references like p[i], where p is declared double*. It’s a common practice and a fundamental part of the C idiom. Despite all this obvious stuff, try passing a reference to the array b, and you’ll find that some pointers are more equal than others. The declaration: Matrix B(b, 3, 3); doesn’t work. How can this be? If no size data are stored anywhere, the arrays a and b look identical in memory (and are perhaps in the same memory locations), and both references are passed as simple pointers, why then does the statement work differently for the two arrays? The answer is simple enough: the compiler knows all. Although it doesn’t find the need to store extra information anywhere, it knows that the two arrays are fundamentally different, syntactically. It won’t let you treat them the same. To do so would be like equating two objects of different types. What we’d really like to do is to define the constructor as: Matrix::Matrix(double a[ ][ ], size_t nr, size_t nc) To those who e-mailed me that the storage arrangement used by the compiler—row-major or column-major—shouldn’t matter to the programmer, I can’t resist pointing out that, in this declaration, it danged well does matter. The three inner brackets in that initializer refer to rows, not columns, and we had better not forget it. What should we make of all this? Mostly, it’s that trying to do useful matrix math in C/C++ is not fun. The C constructs, including esoterics like: *b++ = *a++; but this doesn’t work. The compiler has no way to tell the constructor the number of rows in a. Hence my earlier rant concerning C’s lack of support for conformant arrays. Of course, I can still use the converting constructor with a doubly dimensioned C array. I only have to make sure it’s passed a pointer to its first element. The statement: Matrix B(&b[0][0], 3, 3); work great for singly dimensioned arrays, but not so great for multiply dimensioned ones. I’ve found it best to tell the compiler that all the arrays are singly dimensioned, and keep the truth to myself, hiding the details down inside functions or objects. That’s why I wrote the class Matrix and its ancestors in the first place. One word of caution concerning the converting constructor: there is not (nor can there be) any range checking on the size parameters. If you create a matrix from an array, you’re expected to provide size parameters that don’t violate the range of the input array. If you break this rule, you won’t break the compiler or anything, but you will get garbage elements towards the end of the matrix. Finally, I should mention that, because I’m so enamored with 3-vectors and 3x3 matrices, I sought to have the dimensions of this constructor default to 3x3, independently of the class variables. This would have let me write things like: Matrix A(a); works just fine, albeit a little inscrutable. Before we leave the topic, take one last look at the declaration: without having to pass any size parameters. This, however, turned out to be a Terrible Idea. Because the call looks just like a call to the copy constructor, my compiler tried to typecast the array to an object of class Matrix. I guess I could have overcome this obstacle with use of the verbatim keyword, but what if I had? Then I would have been able to write two declarations: 16 JUNE 2008 | embedded systems design | www.embedded.com http://www.embedded.com
Table of Contents Feed for the Digital Edition of Embedded Systems Design - June 2008 Embedded Systems Design - June 2008 Contents #Include Party Bit Programmer's Toolbox Cover Feature: Virtual Hardware Platforms for Embedded Software Validation Allocating Memory in MATLAB-to-C Code MDD and IDEs: Making the Twain Meet in Embedded Systems Design Avoid a Thrashing Guest Editor Advertising Index Break Points Marketplace Embedded Systems Design - June 2008 Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page Cover1) Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page Cover2) Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page 1) Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page 2) Embedded Systems Design - June 2008 - Contents (Page 3) Embedded Systems Design - June 2008 - Contents (Page 4) Embedded Systems Design - June 2008 - Contents (Page 5) Embedded Systems Design - June 2008 - Contents (Page 6) Embedded Systems Design - June 2008 - #Include (Page 7) Embedded Systems Design - June 2008 - #Include (Page 8) Embedded Systems Design - June 2008 - #Include (Page 9) Embedded Systems Design - June 2008 - Party Bit (Page 10) Embedded Systems Design - June 2008 - Party Bit (Page 11) Embedded Systems Design - June 2008 - Party Bit (Page 12) Embedded Systems Design - June 2008 - Party Bit (Page 13) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 14) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 15) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 16) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 17) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 18) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 19) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 20) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 21) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 22) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 23) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 24) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 25) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 26) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 27) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 28) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 29) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 30) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 31) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 32) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 33) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 34) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 35) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 36) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 37) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 38) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 39) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 40) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 41) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 42) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 43) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 44) Embedded Systems Design - June 2008 - Avoid a Thrashing (Page 45) Embedded Systems Design - June 2008 - Avoid a Thrashing (Page 46) Embedded Systems Design - June 2008 - Avoid a Thrashing (Page 47) Embedded Systems Design - June 2008 - Guest Editor (Page 48) Embedded Systems Design - June 2008 - Guest Editor (Page 49) Embedded Systems Design - June 2008 - Guest Editor (Page 50) Embedded Systems Design - June 2008 - Guest Editor (Page 51) Embedded Systems Design - June 2008 - Advertising Index (Page 52) Embedded Systems Design - June 2008 - Break Points (Page 53) Embedded Systems Design - June 2008 - Break Points (Page 54) Embedded Systems Design - June 2008 - Marketplace (Page 55) Embedded Systems Design - June 2008 - Marketplace (Page 56) Embedded Systems Design - June 2008 - Marketplace (Page Cover3) Embedded Systems Design - June 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.