. |
Last Month's Obfuscated C++
Last month we started with a simple template class that holds an integer matrix:template <int A, int B> struct m {
int _[A][B];
};
You were asked to explain the semantics of this template function:
template <int R, int B, int C>
m<R,C>
f(const m < R,B > & x, const m<B,C> & y) {
m<R,C> rc;
for(int c=R-1;c>=0;-c)
for(int r=0;r<C;++r){
int k=0;
for(rc._[c][r]^=rc._[c][r];k<B;++k)
rc._[c][r]+=y._[k][r]*x._[c][k];
}
return rc;
}
The function f() performs matrix multiplication on the two matrices passed in as arguments. The RxB matrix x and the BxC matrix y are multiplied, giving an RxC matrix result. Each element is computed using the formula:
To obfuscate this calculation, we've used improper indenting, and we've taken care to name the row index c and the column index r. We've also walked through the rows in descending order but walked each column in ascending order. Also note the initialization in the inner for loop:
rc._[c][r]^=rc._[c][r];
Although the initial value of the cell is indeterminate (because rc is an automatic POD structure), it has some value, and exclusive oring that value with itself will zero out the cell.
|
This Month's Obfuscated C++
This month we ask you to play lexical analyzer. What value
is returned from this function?
int x=2,y=3,z=4;
int*a=&x,b=5,c=6,*d=&y,e=7,f=8,*g=&z;
int h(){
return
*a//*//
*b /*//
*c /*//
*d /*/
*e */
*f *
*g;
}
No fair using text editors that chromacode comments!
Rob Murray is Director, Engineering at the Irvine office of Net Explorer, an object-oriented software consulting company based in Houston, TX. He has taught C++ at technical conferences since 1987, and is the author of C++ Strategies and Tactics. He was the founding editor of the C++ Report and can be contacted at [email protected].
Quantity reprints of this article can be purchased by phone: 717.560.2001, ext.39 or by email: [email protected].
|