Embedded Systems Design - June 2008 - (Page 18) programmer’s toolbox Matrix A; // default constructor Matrix B(b); // convert from array that looked almost identical, but worked in fundamentally different ways. The default constructor gets its size parameters from the class variables, but the converting constructor gets them from the default values of the passed parameters. In all likelihood, I’d end up with matrices of different sizes. Not good. So I gave up the notion of default values. If you want to create a matrix from an array, you have to give the dimensions explicitly, as in: Matrix B(b, 3, 3); In class Vector, I provided one more constructor, which was specific for vectors of length 3. It took the three scalar components (x, y, z) of the vector as arguments. I’d like to have a similar constructor that would let me build a 3x3 matrix from its three columns, expressed as vectors. However, to build this constructor, I’d have to include the header files for both the vector and matrix classes, and cross-reference them. I’ve chosen to defer this step until next time. There’s a change coming in the file layout that will make this step much easier. ASSIGNMENT STATEMENTS For this column, I’ve provided one assignment statement, which is shown in Listing 3. As usual, the code contains a couple of subtleties that may not be immediately obvious. First, since the destination matrix already has its array allocated, we must check to make sure it’s the right size. If not, we have to deallocate it and allocate a new one. In past versions, I’ve tended to retain the destination array if it’s large enough, trading speed for a few wasted bytes. However, in this latest incarnation of the class, I’ve chosen to keep things clean by making sure the two arrays have the same size. Note, however, that they don’t necessarily have to have the same dimensions. If, say, one matrix is 3x4, and the other 2x6, that’s okay. Both arrays have 12 elements, and the function will update the size parameters correctly. One important point: when we implement assignment statements that involve objects that depend upon dynamic memory allocation, it’s usually vitally important to make sure we’re not assigning the object to itself. Otherwise, the function could end up deallocating its memory just before copying data from it. Not good. That way dragons be. In this case, however, there’s no problem. If we’re foolish enough to assign an object to itself, the two sets of sizes will match, the if-test will fail, and the delete-new sequence will be skipped. We’ll end up copying data to itself, but that’s an acceptable performance hit. In class Vector, I provided a second assignment statement that took, as its argument, a single scalar value. That value gets stuffed into every element of the array. It’s not a very useful operation for vectors, because I can only think of a couple of cases where a vector with equal elements can be useful. The statement is handy, however, for the construct: v = 0.0; and that’s why I supplied it. A similar function makes even more sense for class Matrix. As with the vector class, a matrix with all its elements set to some scalar number has limited value. However, recall that there’s an entity called the scalar matrix. It’s a square matrix with equal values down the diagonal, and zeros elsewhere. The matrix: ⎡k 0 0 ⎤ ⎢ ⎥ K = ⎢0 k 0 ⎥ ⎢0 0 k ⎥ ⎣ ⎦ (1) has the property that the product Kx multiplies every element of x by the constant k. The scalar matrix is an oftenused construct in matrix math. The statements: O = 0.0; or I = 1.0; Listing 3 The assignment statement. // copy assignment Matrix & Matrix::operator =(const Matrix &a) { if((m*n != a.m*a.n)) { m = a.m; n = a.n; delete[] p; p = new double[m*n]; } vCopy(a.p, p, m*n); return *this; } have obvious value. Aside from that, a scalar matrix is often used as a starting point for more complicated computations. STREAM I/O The finished class Matrix will definitely have this kind of assignment statement, but I’m deferring that until the next column. The last code I’ll show you this month is for the two stream I/O functions. They are shown in Listing 4. There’s nothing fancy about these functions—they’re about as minimal as one could provide. However, they do perform their intended duties. More elegant functions 18 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.