Void pointer is a special type of pointer which can reference or point to any data type. This is why it is also called as Generic Pointer.
As a void pointer can point to any data type, neither it is possible to dereference a void pointer, nor is it possible to do any arithmetic operations on void pointer. Hence an explicit type casting is absolutely must for using a void pointer.
What is a null pointer?
A null pointer is also a special type of pointer which points definitively to nothing.
As per official C language description, every valid type of pointer has a special value called “Null Pointer” and this value is always distinguishable from all other pointer values and it is guaranteed to compare unequal to a pointer of any object or function.
Hold it for a second! So what you are saying is there are different versions of null pointers for every pointer type. Is it so?
No. We cannot call it as a “different versions” rather their internal values are different. But as a programmer, we need not to worry about it because compilers take care of and track the internal values (may be differently depending on the compiler types)
So why don’t you say that a null pointer is a pointer which is not initialized yet?
I cannot go with this idea because an uninitialized pointer may point to any thing. There might be an uninitialized char pointer, an uninitialized int pointer and so on. But a null pointer is definitely something which is there not to point anything.
Ok. Now tell me what is a dangling pointer?
Dangling pointers are the pointers which point to already de allocated or de referenced memory locations. Suppose I write a piece of code like this
int*p;
p = (int*)malloc(n*sizeof(int);
----
----
free(p);
In this piece of code, after allocating some memory to p, we are de allocating the memory which was initially pointed by p. Hence now onwards, p acts as dangling pointer.
How to rectify this?
In this case, after de allocation that is after using free() we should set the pointer p to null as p = NULL
Could you please exhibit any other source of dangling pointer problems?
Well, there might be other sources also, like
Suppose I have declared int*p, and within a specific scope, I have defined another integer x as 10; and then within that specific scope I am making the pointer p to hold the base address of x. Now outside this specific scope the p will act as a dangling pointer. I can write the code as
void foo (void)
{
int *p;
{
{
int x = 10;
p = &x;
}
// here p will act as dangling pointer
}
}
Another case might occur when from a function, we return an address of any variable and a corresponding pointer in the receiver section receives it, then the receiving pointer might get into dangling pointer problem, as just after the returning, as the variable has passed it scope, the memory stack (where the variable is stored currently) might be refreshed. But this phenomenon is not obvious, this might happen.
Fine, so how to deal with this second case?
To deal with this, to be in safer side, we must have the scope of the variable intact out of the function call; declaring it as static might be an option.
A lot of students (count me as well) in their final year of IT course are confused and overwhelmed about their Project development. Final Year IT Projects are a crucial milestone in every IT students educational...
A data structure is a particular way of organizing data in a computer so that it can be used efficiently.
Data structures can implement one or more particular Abstract Data Types (ADT), which specify...
Mathematics is based on formulas and method to use them.The practice make you perfect in this subject.
Prepare according to syllabus, try to solve old papers, revise the concept you learned and try to...
Core java topics
What is Java?Execution Model Of JavaBytecodeHow to Get Java?A First Java ProgramCompiling and Interpreting ApplicationsThe JDK Directory StructureUsing Eclipse
Data types and Variables
What...
Hi,
This is my first lesson for you guys. Hope you enjoy reading it.
In recent community questions, I found many people wanted to be good programmers, or wanted to have good hands on certain language,...