Embedded Systems Design - June 2008 - (Page 15) Listing 1 Some constructors. // default constructor; creates a matrix of default dimensions Matrix::Matrix() { assert(matClassRows >0); assert(matClassCols >0); m = matClassRows; n = matClassCols; p = new double[m*n]; vClear(p, m*n); } // Copy constructor Matrix::Matrix(const Matrix &a) { m = a.m; n = a.n; p = new double[m*n]; vCopy(a.p, p, m*n); } The default constructor and the copy constructor are shown in Listing 1. Note the use of the low-level functions vClear and vCopy, from the file SimpleVec.cpp. More about these functions later. The use of vClear is demanded by the charter of SEUV. ARRAY TO MATRIX Perhaps the most important and useful constructor is the third one, which builds a new vector, copying its data from an ordinary C array. Why is it so important? Because it’s one of the few ways, short of reading an input file, that we can stuff data into the matrix elements. It’s the closest we’re likely to come to an initializer for objects of this class. The constructor, shown in Listing 2, borrows heavily from its cousin in class Vector. The difference is that, in the earlier constructor, the transfer of data is a simple element-by-element copy from one singly dimensioned array to another. In the new constructor, the transfer of data is a little more subtle. It also goes back to that issue of how array data are stored in memory, which I discussed in my last column. As before, it’s worth taking the time to discuss the details. As we do so, we’ll discover one last “gotcha” that the C syntax has in store for us. Look carefully at the declaration of the constructor. The input array is declared to be double a[ ], that is, an ordinary single-dimensioned array of doubles. To initialize that array, I might write: double a[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Listing 2 The converting constructor. // Converting constructor from array Matrix::Matrix(double a[], size_t nr, size_t nc) { m = nr; n = nc; p = new double[m*n]; vCopy(a, p, m*n); } The declaration: Matrix A(a, 3, 3); would then construct a 3x3 matrix. In the process of building the matrix, we have transformed the shape of the data item from one-dimensional to two-dimensional. Does this seem a little strange? Couldn’t we, instead, define the array: double b[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Now, you and I and the designers of C and the ISO/IEC C++ Standards Committee know that the two declarations have identical results. The C language doesn’t www.embedded.com | embedded systems design | JUNE 2008 15 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.