Archive for November, 2007

Function Pointers

Declaring function pointers in C\C++ has a somewhat strange syntax, specially when you want to specify a function pointer as the return type of another function.
so here is a brief explanation:

To declare a pointer to an int called pNumber:

int *pNumber;

To declare a pointer to a function called pFunc (that has a string parameter and returns an int):

int (* pFunc)(string);

in red is the variable name, in purple is the type.

To declare a function that returns a pointer to a function:

int (* GetFuncPointer(void) )(string);

in red is a normal function declaration, in purple is the return type of that function.
S o basically int(*)(string) is the type of the function pointer.

If we want to declare a function that takes a function pointer as a parameter then we would write something like this:

void RegisterCallBack( int(*)(string) ); /* unnamed parameter */

or

void RegisterCallBack( int(*myCallback)(string) ); /* named parameter */

A prettier way to use function pointers is to use typedef to declare an alias. The typedef syntax is exactly the same as declaring a regular variable (or a function pointer).

typedef int(*FunctionPointer)(string);

now we can use FunctionPointer like this:

FunctionPointer GetFuncPointer(void);

Click For More Information…

Mr. Compiler, would you please inline this function?

I’ve always read that marking a function inline doesn’t guarantee that the compiler will actually inline it, but it’s merely a hint to the compiler to “try its best” to do it. Which have always raised the question in my head, how do I know if Mr. Compiler agreed to inline my function? Do I have to look at the generated assembly code? There has to be an easier way!

So I invested 15 minutes researching that issue and found out that Mr. Compiler is friendly enough to let you know if it was not possible to fulfill your request to inline a certain function by issuing a warning.

For Microsoft’s VC8 compiler warning C4710 is turned off by default, you can turn it on by doing any of the following (depends on your choice):

  1. #pragma warning (default : 4710)
  2. #pragma warning (custom_warning_level : 4710)

For GCC the -Winline switch can be used to achieve the same thing.

Installing nVidia drivers on debian linux

Since I forget the steps every single time I try to install or upgrade linux I’ll write them down.

  1. download the drivers from www.nvidia.com (usually a file with .run extension)
  2. download the kernel header files — apt-get install linux-headers-$(uname -r)
  3. execute the command — sh path to the nvidia driver here

There is another method I found, that seems to work for a lot of people:

  1. apt-get install module-assistant
  2. m-a prepare
  3. m-a auto-install nvidia