Shinobu’s Secrets

September 29, 2008

Poor Man’s Debugging in C++

Filed under: C++ — Shinobu @ 6:53 am

Disclaimer: I suspect that this post won’t be useful to anyone who really knows how to debug a program — but hey, the procedure I describe is so simple, and has no learning curve!

I am a novice C++ programmer. Recently, I’ve been using a simple trick to see which portions of my code are being accessed or not. Simply include the cassert header in your code, like so:

#include <cassert>

Now, include the statement std::assert(0); in the part of your code that you suspect is causing your program to crash. The assert() function (or macro, rather, as Wikipedia suggets) aborts your program if the condition given is zero or false — so if we hard-code a failing argument, as in assert(0), your program will terminate consistently, upon executing that particular assert(0) statement.

The cool part: if your code executes the assert(0) statement, then that means that, up to that point in your code, your program worked perfectly fine. On the other hand, if you cannot get your program to abort with assert(0), this means that your program hangs or crashes before getting to the “assert(0)” statement ; thus, you can just move this “assert(0)” statement up (“up” as in an earlier point of time in the manner your code is executed), and repeat the procedure above to get closer to the bug.

So, for example, if you have a FOR loop, and inside this FOR loop, the first thing you put in is assert(0), and nothing happens, this means that the FOR loop isn’t even being executed at all! Similarly, simply put in assert(0) inside a function, to see whether that function is being called; if it IS being called, your program will abort with assert(0).

As a general practice, you could put in many assert() statements in your control flow code, so that they only execute if the statement inside assert() is true.

July 12, 2008

Netbeans 6.0.1 on Elyssa – Ruby, Rails, C/C++ Projects

Filed under: C++, Linux, Rails, Ruby — Shinobu @ 8:15 pm

Some weeks ago, the Ubuntu people quietly upgraded their Netbeans package on their repositories from version 5.5 to 6.0.1. For me, this is great, because I don’t have to download shell install scripts from the Netbeans website any more (the current 6.1 not being much of a facelift from version 6.0.1). However, you’ll notice that 6.0.1 from Synaptic does not support the creation of projects other than Java or some other “Samples” in the New Project dialog box. To remedy this, simply go to Tools -> Plugins -> Available Plugins, and check off the boxes that you want to use. For me, I added the Ruby and Rails, Ruby Extra Hints, and C/C++ plugins. Now, I can create (and open existing) projects that are based on Ruby, Rails, or C/C++, and use all of the functionality of the IDE associated with those languages! You might also want to go to the Tools -> Plugins -> Updates tab to install some of the “core” updates to the general IDE.

Blog at WordPress.com.