Tips & Tricks
In this page I'll collect a list of tips & tricks about C and C++ programming.
Send your preferred with a mail to I.Vighetto@lakesnet.it
or silicontiz@geocities.com. If you include your homepage URL,
it will be added to this page (more tips, more links!).
-
C++: Quick commenting and uncommenting
If you have to enable and disable repeatedly a piece of code without
insert and delete every time the four characters /* and */ around the code,
is possible to use this technique:
/*
some stuff to enable/disable
//*/
In this manner, to enable the code simply add a / before the /* transforming
the first line in //*.
-
C, C++: Exchange two variables without using a temp
Normally, to exchange two variables (a, b) we use a third variable
as a temporary value:
int tmp = a;
a = b;
b = tmp;
Exists another way to achieve the same result using the exclusive or
operator:
a ^= b;
b ^= a;
a ^= b;
It is possible to use this technique on more complex structures iterating
on single byte, word or dword.
-
C: Declarations at the beginning
A secondary benefit of C++ over C is the possibility of declaring an
automatic variable eveywhere in a function (with benefits in terms of
legibility and information hiding). It becomes quickly an habit and when we
return to C programming, is very easy forgetiing of place declarations at the
beginning. C compilers in this case reports strange errors. Pay attention to
this little difference.
-
C, C++: Sizing of bidimensional arrays
Accessing to an element of a bidimensional array involves a lot of slow
multiplications: to speed up thhe element addressing is possible sizing arrays
with powers of two and using bit shifts instead of multiplications.
For example:
int iArray[16][28];
becomes
int iArray[16][32];
There's a little waste of memory but a good performance increment.
-
C, C++: Optimize of boolean operations
Using Boole's algebra, is possible to optimize a test like:
if ((x & 1 ) || (x & 4)) {...};
(with the associative propery of AND operator with OR) and becomes
if (x & 5) {...};
and in a smilar way:
if(x >=0 && x < 8 && y >= 0 && y < 8) {...};
after optimization:
if(((unsigned)(x | y)) < 8) {...};
The cast to unsigned is important to avoid the sign bit.
It's easy remember these rules thinking & as a multiplication and | as
an addiction and using the usual properties.
-
C++: Incomplete declarations
When a class only refers to another class without ever dereferencing it, incomplete declaration will help improving the situation.
// fragment from myclass.h
#include "someclass.h" // defines class SomeClass
class myclass {
SomeClass *getPtr();
private:
SomeClass *ptr;
};
becomes
// fragment from nicer myclass.h
class SomeClass;
class myclass {
SomeClass *getPtr();
private:
SomeClass *ptr;
};
The #include directive is replaced with a incomplete declaration of SomeClass. This is possible only when there are pointers to SomeClass without dereferencing it. The advantages are: is possible change SomeClass without recompiling myclass, compilation of sources that include myclass is faster because doesn't require reading of someclass.h
Return to index