Eclectic

About programming and maybe more…

Archive for February 21st, 2007

Use assert()

Posted by tread on February 21, 2007

Sadly, very few people do.

What it does is notify you if you reached some logically incorrect state -e.g., if a pointer is NULL, you definitely do not want to continue.

Steps:

include <assert.h>

assert (expression);

// if expression evaluates to false, then the program aborts

// expression can be anything, say (if ptr != NULL)

If the expression does evaluate to false, then you get a message like “assert failed in function foo at line x” – rather useful. Better sprinkle all your code with this – anyway, you can turn it off in production executables by adding

#define NDEBUG

in your code. An easier way – one that doesn’t require source modification – is to pass -DNDEBUG as a parameter when compiling the file.

e.g.,

gcc -c test.c -DNDEBUG

Posted in Programming | Leave a Comment »

HOWTO: use aptitude instead of synaptic and why.

Posted by tread on February 21, 2007

This post is an old article I once posted on ubuntu forums – I’m getting it here for my own archival.

What is aptitude?
Aptitude is a package manager for Debian and Debian based systems (similar to say, synaptic).

Why should you use aptitude over synaptic/apt-get?
The million dollar reason: it remembers dependencies downloaded to install a particular package and removes them automatically when you remove that package. Synaptic only gives you the history log, and apt-get not even that.
For example:

I install wmaker using synaptic, it installs libwraster along with it.
I purge wmaker out using synaptic, it leaves libwraster. I have to manually search for it and uninstall.

Now this may not seem a big deal, but imagine if wmaker had 20 dependencies.
On the other hand:

I install wmaker using aptitude, it installs libwraster with it.
I purge wmaker out using aptitude, it figures out libwraster isn’t needed anymore, and gets rid of that too.

Awesome, isn’t it? :)
But I held back from using this rather nifty tool because I didn’t want to learn how to use it. Now I can use it, at least to do simple tasks. So I thought I would share that knowledge.

aptitude can run in two modes:

  1. command line mode
    I will not write about this, other than to say the commands stay like apt-get .. which means you get aptitude power with apt-get commands.
    For eg.,
    sudo aptitude install wmaker
    sudo aptitude update
    sudo aptitude upgradeNote: to do a default upgrade, use the commandline above. Pressing U in the gui seems to do a smart upgrade, and that isn’t something I always want.
  2. gui mode
    You start this with: sudo aptitude
    f10 : pulls down the menu
    Note: it seems f10 pulls down the gnome-terminal menu. If you want to use f10, then you may need to turn off the gnome-terminal menu for the session. Alternately, just click on the aptitude menu, the mouse click works.
    q : quit
    u : updates package lists
    / : search for a package (firefox style)
    n : find next
    f : forget new packages, useful if you have a habit of peeking into new packages available after each update.Once you have found your package,
    enter: to see information about that package
    + : mark a package for install or upgrade
    - (minus) : mark a package for removal
    _ (underscore): mark a package for purging
    : : cancel any action on the selected packageU: to mark all upgradable packages (for upgrade :) )
    Once you have marked your changes,
    g : apply those changes.
    If you are in a screen which says displays package information, and you want to go back to the earlier screen, press f6 or q.

Once you press g, all the changes get listed. You can modify any of the changes, and then press g again to finally apply marked changes. I’ll keep adding to this HOWTO as I learn new things, and if you have any suggestions/tips/corrections please add to this thread .. I’ll update this post.

Posted in Ubuntu | 3 Comments »