In general you cannot build an efficient data structure without pointers and reference.But the same pointers and reference can make things screw up also, so be careful while handling them.

What is Pointer ?

A pointer is a variable which holds the address of some variable . Obtaining the value of the variable through pointer is called de referencing.

int *p;
int a=20;
p=&a;
cout<<*p;

Null Pointer :

A null pointer has a reserved value, often but not necessarily the value zero, indicating that it refers to no object. Null pointers are used routinely, particularly in C and C++ where the compile-time constant NULL is used (though the integer literal for zero is preferred in C++), to represent conditions such as the lack of a successor to the last element of a linked list.

int *p=NULL;

Note:

An attempt to dereference a null pointer usually causes a run-time error. If this error is left unhandled, the program terminates immediately. In the case of C, execution halts with a segmentation fault because the literal address of NULL is never allocated to a running program. In Java, access to a null reference triggers a NullPointerException, which can be caught by error handling code, but the preferred practice is to ensure that such exceptions never occur.

Funny quote on null pointer : "your worries on useless things is like dereferencing a null pointer and ending up with runtime error or with a segmentation fault."


Double indirection : If a pointer refer to another pointer it is called double indirection. We need to derefer the last pointer twice to get the value of the variable.

int **p1,*p,a=20;
p=&a;
p1=&p;
cout<<**p1;

Wild Pointer :
Pointers that have not been initialized (that is, a wild pointer does not have any address assigned to it) and may make a program crash or behave oddly when it is accessed.


Smart pointer : When the general pointer is made to handle automatic garbage collection or bounds checking it is called smart pointer.
Smart pointers try to prevent memory leaks by making the resource deallocation automatic: when the pointer (or the last in a series of pointers) to an object is destroyed, for example because it goes out of scope, the pointed object is destroyed too.


Segmentation Fault : A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to write to a memory location where only read is allowed, or to overwrite part of the operating system).


Some common code cause segmentation faults are :

int main(void)
{
main();
}

int* ptr = NULL;
*ptr = 1;

Dangling Pointer :
This arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.

int main()
{
char *dp = NULL;
/* ... */
{
char c;
dp = &c;
} /* c falls out of scope */
/* dp is now a dangling pointer */
}
To handle such case assign the pointer dp to null before coming out of inner loop.

The above things are for those who knows well about pointer, but they are lost in the course of time. More info can be obtained from wiki

Related Posts :



Bookmark and Share