Making the Compiler Work Harder for You

In the last post about learning to use the debugger, I mentioned a few tricks that you can perform with a good debugger, like for example changing the values at certain memory locations. These are tricks that can speed up your debugging process, because you don’t need to recompile lots of code and restart your program just to test a small change.

Another related technique consists of using the compiler itself to perform some check while you make changes to the code

An example of how this works is presented in a post by Hovik Melikyan. He discusses what he calls brute force programming, where the idea is to use the compiler to perform temporary checking.

For example, one of the techniques is to use the compiler to figure out exactly where a variable might be used. The simple thing to do in this case is just to rename the definition of the variable temporarily. During compilation, you will see all locations where the variable was used and fix them accordingly.

A related technique that I use in C++ can be used to show where a type was defined. If you want to find the definition of a type (usually from a library, such as MFC, for example), what you can do is just writing a new typedef with the type you are searching for. If the type is, say, Widget, you would then write

typedef int Widget;

If you do this, the compiler will immediately give you an error saying something like: “type Widget was previously define on file X, line Y.” So, now you know where the type was originally defined.

Is there any other trick that you use to make your compiler work for you? Let me know.

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

2 Responses to “Making the Compiler Work Harder for You”

  1. Eclipse CDT (and indeed any competent IDE, I don’t know about Visual Studio…) will index typedefs and let you find their declaration and, for classes, their inheritance. It will show you everywhere a variable/field/method is used. These apply for Eclipse JDT as well. I don’t know why you think you need those hacks, but you’re not using your IDE properly, or not using a proper IDE.

    By Dmitri Nikulin on Jul 25, 2009

  2. Ok!!! A good IDE will point out this issues properly. But, IDE is generally heavier than a file editor like emacs/vi. If you use emacs/vi, one solution can be generate TAGS file using etags or ctags, respectively. However, the tips is valid and certainly util.

    By UilCoder on Feb 16, 2012

Post a Comment