Saturday, October 9, 2010

Valgrind for dummies to experts

Well... not really for experts; experts will know better than me.

Valgrind is a framework for dynamic analysis tools. As far as we are concerned, it is something that helps us remove those nasty segmentation faults and unnoticeable bugs.

If you believe that there is some wrong array indexing somewhere but can't find out where, then valgrind is the perfect tool for you.

The use of valgrind is simple.

1. Compile your code with the -g flag.
gcc abc.c -g -Wall
OR
g++ abc.cpp -g -Wall

2. Run with valgrind
valgrind ./a.out
OR
valgrind ./a.out <input

That's it.

Now what you need to know is how to understand the output.
The first few(around 5) lines are not interesting to us.

After that you will see the output of your code and the list of errors.
Most of the time, you should be looking for these lines :
1. Invalid write/read of size 4
It mostly means that you are accessing array index beyond the size of the array.

2. Conditional jump or move depends on uninitialised value(s)
It means you are having something like this :
int a;
if( a > 5 ) { // do stuff
}

You have not given any value to a.
If you try to print some uninitialized value, you will get this error message 4-5 times. You can identify it by seeing that there is a "printf" in each of these.

The most important part is the line just after the above messages. It looks something like this :

==8050== Invalid write of size 4
==8050== at 0x804841A: fun (val.c:5)
==8050== by 0x804844A: main (val.c:15)


It means that the error was at line 5 in the file abc.c, inside the function fun(), which was in turn called by main at line 15.

Valgrind is not perfect in finding array index overflows, especially when it comes to static arrays. Sometimes, running it with the following option may give better results :

valgrind ./a.out --tool=exp-ptrcheck

Wednesday, September 1, 2010

Some copying tips

So we all know how assignments are done. One guy faithfully does the whole thing and the rest copy it and change sentences of the form "Let X be a subset of Y" to "Let Y be a superset of X", etc. Now, very often, you will have to make the same kind of changes several times. Here are some tips on how you can do it efficiently using vim.

We'll deal with 2 examples, each of which use many useful commands.

I)

Suppose I want to change (A) to (B)
(A)
printf("%s is a X\n");
printf("%s is a Y\n");
printf("%s is a Z\n");

(B)
printf("X : %s\n");
printf("Y : %s\n");
printf("Z : %s\n");

What we will do here is to make the changes in the first line and store it in something called a macro. Once you are done, just execute the macro in the other lines.

Steps :
Start in escape mode. You should be at the beginning of the first line when you start.
1. qa : q starts recording the macro. It takes an argument which is the register in which you have to store it. Here we're using 'a'. You can use any letter as the macro.
2. f% : f searches for a character in the same line, starting from the current position. Here we search for '%' to go to that position.
3. d4w : d stands for delete, w for word. Delete 4 words.
4. f\ : Move to the position \. (We want to start editing there).
5. Go to insert mode and start typing normally (add the " : %s"). The recording is still going on.
6. j0 : Move to the next line. 0 moves to the beginning of the line (we want this so that the search for % the next time works properly.)
7. q : Stop recording.

Now your macro is ready in the register a.
8. 2@a : Execute the macro 2 times.

There you go, (A) is changed to (B). Of course, this will be feasible only if you have a large number of lines to do this. The individual commands should be useful even otherwise.

II)

Suppose you are editing an html file where you want to give a drop down box of 30 values as follows.

<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>

and so on until 30.

Steps :
Start in escape mode. Type the first line normally. You should be at the beginning of the first line when you start.
1. qa : start
2. yy : copy the entire line
3. p : paste the line
4. f" : move to "
5. l : move left once.
6. Ctrl-A : This increases the number under the cursor by 1.
7. f : move to the next space.
8. Ctrl-A
9. q

Now 28@a should fill everything upto 30.

Wednesday, August 25, 2010

Youtube downloader for linux

There are many softwares/sites out there that can download videos from youtube and other similar sites. However, here is a much simpler trick which works on most linux distributions.

Whenever you play a video on youtube, the flash file is stored in /tmp as something like Flashxxx.

So if you want to save some video, simply watch the whole video on youtube and do " ls /tmp/Fla* "
The most recent video will most probably be the video you want.

Tuesday, February 23, 2010

vim tips :wq

Note: The time taken in reading this post will be compensated if you practice it properly :)

Most of my friends use Vim as a text editor. However, I find it strange that even after using it for a year, they still use it like notepad! So if they want to delete the second half of line 23, they press 'i', press the down arrow key till it reaches line 23, press the right arrow key till they reach the half point, and then finally press the delete key till the whole line is deleted. One of my friends stopped using vim because he couldn't use his mouse!!

I wont blame them. The vimtutor is an interesting place to start learning vim. But beyond that, the next place that can help you learn further would be some large reference book. So this series of blog posts are intended to slowly get you started with vim.

Let's start with the basics.

1. dd - Delete the whole line.
Similarly, cc deletes the whole line and changes to insert mode. It's d for delete and c for change.

2. D - Delete the rest of the line (starting from the current position). C does the same, and puts you to insert mode.

3. dw - Delete word. cw is similar.

4. % - Move to the matching bracket of the bracket at the current position. d% will delete from the current bracket till it's matching bracket.

5. f - Find. So if you type fr, it will go to the next occurence of r in the current line. If the letter doesn't occur in the current line, nothing happens. F (capital f) will do the same, but find the previous occurence of the letter in the current line. f and F are most useful when you use it along with d. For example, if you want to delete till the next r in the line (including the r), all you have to do is "dfr"!! t and T also work in the same way except that dtr will delete till the r, but not the r. Using c instead of d will do the same, and also put you into insert mode.

6. . (dot) - Repeat the last editing command.

7. u - Undo

So next time you want to change

draw_dog(int tail) to draw_human(int brain), just go to the first d, type fdc%human.... .

More to come soon.

Tuesday, January 26, 2010

Hello World

Hello, World!\n

There have been times when I came across something interesting and wanted to record somewhere. And that's how this blog was born...

My main interests are algorithms and C++. I like learning about the latest technology, though I don't use any kind of fancy gadget.

I own a Fujitsu ESPRIMO v6545 laptop and use openSUSE 11.2 (No windows even through dual boot). That should be enough to show that I don't follow others blindly.

You can expect some interesting vim tricks here.