Thursday, August 05, 2010

Note to myself

Class Base{
public:
int a;
void seta(int x)
{
a = x;
}
};
Class Derived : public Base{
public:
Derived(){
a = 0;
}
int a;
};

main(){

Derived D;
D.seta(1);
cout << D.a << endl; --> this will print 0 only 'cos you just called the base class function which just sets its copy of variable 'a' which is masked by derived class varible having the same name.
}

Monday, January 04, 2010

Find out which process is keeping a resource busy in Unix

http://ocaoimh.ie/how-to-umount-when-the-device-is-busy/

Tuesday, May 13, 2008

Wanna see how your MACROS are expanded?

#define

#include

#if

#ifdef

These, you know, are some of the preprocessor directives that we normally get to see in any program. There times when you feel its necessary to see what exactly the preprocessor has understood from the directive. For example, you might wanna know if your MARCO got defined as expected. Consider the code below.

 

#define MACRO1 1 + 2

#define MACRO2 1 - MACRO1

int main(){

int a=MACRO2;

cout << a << endl;

return 0;

}

To see the preprocessor output, use the gcc with -E option to compile the code. The output will be printed on screen itself.

[balaji1@cheud216][/home/balaji1]$ gcc -E temp.C

# 1 "temp.C"

# 1 "<built-in>"

# 1 "<command line>"

# 1 "temp.C"

 

int main(){

int a=1 - 1 + 2;

cout << a << endl;

return 0;

}

Likewise, if you have a #include <iostream.h>, you'll get to see the whole of iostream.h printed along with your code after 'gccing' as mentioned above.

 

The -E option can be used with any other compile time option in gcc (or g++). In case a make command is used for compilation, you may get to see gcc or g++ being used with many other options to compile a file. Just include -E to all those options and do a compilation again to get the preprocessed output.

Submarine cable systems across the globe

I ran into this link  on 10/14/2007.
 

You can find all the major submarine cable networks in the world.  
 
 
Click on the LOOK button in the bottom left hand corner of the graphic to see a flash animation of how the network got laid. Also check out NEC's other projects too (the links below Our Record in the left hand corner).

Killing all processes

Here's the command to kill all process belonging to current user in solaris.
 
kill -9 -1
 
where 9 => number equivalent for SIGKILL
 

Tuesday, April 08, 2008

Handling kill signal in shell script

Trap command is a cool utility that handles all hardware signals in a shell script.

trap "echo CtrlC ;exit" SIGINT -> This will echo CtrlC before exiting!!
trap "echo STOP ;exit" SIGTSTP -> This handles a stop signal (ctrl+Z) !!

Running a script in debug mode

The first line of the script should include a -x, for the script to run in debugging mode.

#!/usr/bin/bash -x

Monday, March 20, 2006

Templates.... the next level of overloading

'Templates' is one concept that i learnt today... Templates give awesome flexibility and generic nature to programs ... Its the next level to overloading.. There r two types... Class templates and function templates... A function template is more like overloading but the advantage is u dont have to declare different functions for different datatypes.. If your function logical does the same irrespective of the type of operands, then templates come in handy to save LOCs and improve readability. A class template helps u delcare a class that can vary in member elements types/arguments depending on how an object of that class is declared. Its a concept great enuf that it had a full workshop on it.
Here's more on Templates

Tuesday, February 14, 2006

delete X,Y will only delete X

If u need to delete two objects, u gotta use two delete statements...

delete X,Y wont work.

Instead one needs to use

delete X;
delete Y;

Static member function Vs non-static member function.

Was wondering whats so special about a static member function before I discovered two things.
1. A static member function can be accessed using two sytaxes.

class_name::function_name
OR
object_name::function_name

2. A static member function can only access static member variables (not only those that belong to its class, but also those that belong to other classes).
Now why is there such a restriction?

Rule 1 says that a static function can be accessed even without an object name. means it can be accessed even if no object of that class is declared. That being so, if that function is allowed to acces non-static member variables, what will it do? Let Roll_no be a non-static variable in a class named student. Lets consider a static function func that tries to access Roll_no. Now when func is called and there are no or more than one objects of class student are declared, whose Roll_no will it get ? Thats why this restriction.... A static member function can not access non-static member variables .

Tuesday, February 07, 2006

The speciality of a virtual destructor

A virtual function in a class, is one which allows modification of its functionality by the derived classes. But whats so special about a virtual destructor is that, when an object of a derived class is destroyed, both the base class destructor and the derived class destructor are invoked. This wont happen if the destructor of the base class is a non-virtual function.