Emacs and GDB Cheatsheet

March 29, 2013

How to switch screens in Emacs ?
(Control + x) then press o

When you have to set a breakpoint, go to the window showing the code. Come to the line where you want to put the breakpoint. Press (Ctrl + x) then space.

There are three other commands that you will find useful: (1) undisplay followed by a number n will remove item n from the display list. (2) print followed by an expression will evaluate and print the expression once (without adding it to the display list). (3) step is similar to “next”, executing one statement. The difference is that the step command tries to step into the body of each function, and execute the lines of each function one at a time. Since heatwave has no functions, we’re using next. (Also, step would cause problems with heatwave, since the debugger would try to step into the input >> and output << functions.

How to log gdb output to a file ?
set logging file mylog.txt
set logging on

Click to access gdb-refcard.pdf

Click to access emacs-refcard-a4.pdf

Redirect the output of gdb to a file

March 29, 2013

set logging file filename



Automatically load bashrc on login

March 29, 2013

You can add the command “source .bashrc” in a file called .bash_profile which can be put in /home/sayghosh. That will automatically load your .bashrc on login and you wont need to load that separately everytime. 

Differences between C++ and Java – transitioning to C++ from Java

March 29, 2013

The first is that there can be global functions, functions which are not methods of a class, such as printf and main. The next thing you may see is that we have a #include statement. This tells the compiler to read in a file that usually contains class or function declarations. Third, notice that the Java program includes a package declaration, whereas C++ has no analagous concept of packages.

The biggest difference you will immediately notice is that while all function definitions are included in the class declaration in Java, they are usually put in separate files in C++.

Another minor difference you may have noticed is that in Java, main takes one parameter: an array of Strings. (In C++, we use a char* instead of a String, but this will be explained later.)

Unlike Java, C++ has no notion of package friendliness, as there are no packages. Because of this, protected members are only accessible to subclasses, while in Java the whole package can use protected members. As you’ve probably noticed, you put members in sections by their protection level. You can have as many sections of each protection level in a class declarations as you would like. If no modifier is specified, the protection lev


In Java, methods are declared and defined in the same place. It is possible to do this in C++ as well, by defining methods in the header file. This is known in C++ as inlining. Inlining is typically used only for very short methods such as accessors and mutators. For short routines, C++ can frequently optimize things which are declared inline by inserting the code for the routine directly in the calling function, thus avoiding the large overhead of calling a function. So, it is often to your advantage to inline definitions of accessors, mutators, and other short, frequently called functions that don’t require use of an external library.

Hint for later: When you learn about pointers and start overloading things so they take either a pointer type or an int, the symbol NULL is actually an int! This has brought down many a great C++ programmer. The workaround is to explicitly cast NULL to the pointer type you want.

Yes, classes in C++ can inherit from more than one superclass. You should be aware that this is possible and that it is occasionally used. However, multiple inheritance leads to many troubling issues that you don’t want to deal with. Avoid doing it and we will all be happier people. As you gain experience with C++ you will hopefully learn where it is appropriate and where it isn’t.

Note: You could make a class with only pure virtual functions and use it with multiple inheritance to get the same effect as interfaces have in Java. But wait until you know a lot more about multiple inheritance first!

Note two: In Java, you could create an interface when you wanted to group a bunch of constants together. Classes then implemented that interface to use the constants. In C++, instead use a separate header file to define constants. Include that header file in the program files where you need the constants.

The method toString() is defined in class Object and overridden in class String. In the above example, Java knows that obj is really of type String, so at it calls the String.toString() method. (This is polymorphism at work.) It can resolve which method to call at run-time since in Java, all methods are virtual. In a virtual method, the compiler and loader (or VM) make sure that the correct version of the method is called for each particular object.

As you can imagine, making everything virtual by default adds a little overhead to your program, which is against C++’s philosophy. Therefore, in C++ functions are not virtual by default. If you don’t declare a function virtual and override it in a subclass, it will still compile even though the “correct” version of the method may not get called! The compiler may give you a warning, but you should simply remember to do this for any function that you may override later.

The destructor should always be declared virtual. The brief explanation as to why is say you later go back and derive from a class with a non-virtual destructor. When that subclass gets destroyed the results are “undefined” according to Stroustrup. Not all of the memory may end up being released because all of the destructors won’t get called. If you don’t understand that, it’s okay. Just make sure you always make destructors virtual to save headaches later on.

Just like in Java, a class derived from Foo cannot be instantiated unless all pure virtual functions have been defined. Also like Java, intermediate abstract subclasses that don’t define their parent’s pure virtual methods need not list them in their header file.