Friday, August 24, 2007

NULL pointer and Some thoughts C++


Its me again , now came with a NULL pointer in hand... You may think what so special about NULL pointer , right ? .

it is very special . for example even if multiply two NULL pointers ,you will get a the result as a NULL pointer.. is it interesting ? .. :) I was just joking.. 

I wrote this beacause mostly people checks in destructor something like this

if(p) delete p;

 This is not needed , you can just avoid that checking, because delete NULL , will not cause any probles.
so if you have more pointers in your class to delete , avoid that checking is too good to see.

I think Now it is good to mention about this function  IsBadWritePtr . This function can be used to check
whether one address is good to write. In MFC there is function AfxIsValidAddress which will call this function to check.

The next thing about NULL pointer is that , You can still call member functions with null pointer, Like

CCLemon *p = NULL;

p->Drink() ;

Still this will works . but only one condition , Drink should not access any member variables of class CCLemon , and Drink shouldn't be a virtual function. If Drink is a virtual function result is Bhooommm!!!





7 comments:

Girish said...

One good practice will be to assign a null value to the pointer(make it a null pointer) after freeing/deleting it.This will prevent any kind of crashes which could occur by accidently calling free() or delete on it at a later time.

Girish said...

I am not sure about this, but I guess Isbadwriteptr and similar functions uses exception handlers inside them to find whether the memory is accessible .This will result in performance impact if called frequently/or should be avoided completely when performance is of high priority.Could u pls confirm this,KD???

msnkd said...

It is a good practice initialize variables in constructor. you are correct Isbadwriteptr exceptions to distinguish the pointers.But it can be used in situations like when we are using some 3rd party Buggy libraries . Microsoft also has the same opinion.

msnkd said...

It is a good practice initialize variables in constructor. you are correct Isbadwriteptr exceptions to distinguish the pointers.But it can be used in situations like when we are using some 3rd party Buggy libraries . Microsoft also has the same opinion.

Girish said...

I wasn\'t talking about initializing the pointers,that is important .I was talking about some situation like this:char *p=(char*)malloc(20);...................................if(i==2){//some condition    free(p);}...............................free(p);/*freeing p forgeting the above condition at the end of the program/in destructor.This could even be exploited to have  arbitary code execution in certain situations.*/return;}Isbad... series of functions are very usefull when used in debug builds where u could put them in assert statements.

msnkd said...

hi no-name,it may be useful. But i think in the above code it can be avoided as a good practice .

Akhil said...

Working because if it is inline, right?