tags:

views:

214

answers:

5

Can any body explains about the different categories of pointer(like wild pointers)?

+1  A: 

Here is your answer:

C++ Pointers

Mr.Expert
+7  A: 
  • Valid pointer: one pointing to a real object in memory
  • Invalid pointer: one pointing to memory that is not what it is supposed to be.
  • NULL pointer: A pointer whose value is 0 and thus points at nothing.
  • Dangling pointer (also sometimes wild pointer): one pointing to memory that has been deleted/freed.
  • Smart pointer: Not really a pointer at all, but rather an object which acts like a pointer, but manages the memory for you.
Steve Rowe
AFAIK NULL pointer's value needs not be zero (the bit pattern may vary between machines), though the NULL constant evaluates to zero.
Michał Trybus
I could classify "Dangling" and "Invalid" as the same thing. Null too, really, but its verifiably invalid.
Dennis Zickefoose
But logically they're different. A pointer is invalid if you've run out of bounds or mistakenly assigned it to the wrong address, and dangling if you forgot it had been freed already
Michał Trybus
In the upcoming C++ standard, `nullptr` will be the value of null pointers.
mxp
@Michał: in most (about all modern) architectures, `NULL==0`. But there are some architectures where it was not the case. Even worse, on these architectures `NULL!=false` so you can't even check `if(pointer)` for non-NULL there.
SF.
@SF: Not in C++. `0` is always the null pointer, for all pointers. `0` need not correspond to the memory address 0, but the compiler will hide the details there. So `0` is always safe to compare a pointer to.
Dennis Zickefoose
@SF: i thought you can always check `if(pointer)` as this is interpreted as `if(pointer != 0)` and comparison of a pointer and a *constant* zero is treated as a comparison to nullptr (no matter what it is). That's for us not to worry about these architectures where the null pointer is not zero.
Michał Trybus
Always thought that there is only one category - a memory address. I'm still thinking this way. Extra classification doesn't give benefits, so it isn't needed.
SigTerm
@SigTerm: that fails to address NULL, which isn't a memory address or at least doesn't behave like one.
MSalters
@SigTerm: While one might be able to justify ignoring the 'valid/invalid' classification, you are simply wrong about all pointers being simple memory addresses. At the very least, the difference between a raw pointer and a pointer to member can't be ignored.
Dennis Zickefoose
A member pointer isn't a pointer in C++.
Johannes Schaub - litb
@MSalters: NULL is zero in C++. It is a macros, and doesn't have special meaning like nil in python or pascal.
SigTerm
SigTerm
MSalters
+5  A: 

...adding orthogonally to Steve's answer -

  • pointer to a variable (int*, char*, also void* which is a special pseudo-type which must always be cast before dereferencing. Also pointers to pointers.)
  • pointer to a function (void(*)(), int(*)(int)) and so on.)

The distinction is important because one family can't be cast into the other, and vice versa.

SF.
+6  A: 

There are also function pointers, which point to code rather than objects. And pointers to members, which require an instance of a class to fully dereference. And pointers to member functions, which point to code and requires and an instance of a class to fully dereference.

There are several different types of smart pointers as well. These are used to wrap up dynamic memory allocations, and track ownership of the underlying data. When nobody owns the data any more, the dynamic memory is automatically released. The three biggies here are scoped pointers, shared pointers, and weak pointers. Scoped pointers only have one owner, and when they go out of scope they get deleted [see std::auto_ptr in C++03 and std::unique_ptr in C++0x]. Shared pointers can have many owners, and the memory doesn't get freed until all the owners are done with them. Weak pointers are a form of these, but they don't maintain strict ownership at all times; they request ownership when they need it, and that request is denied if the corresponding shared pointer has run out of owners.

Dennis Zickefoose
+1 for pointers to members;)
Michał Trybus
Pointers to members require an *instance*, not a class to dereference.
David Rodríguez - dribeas
Sorry, yes, you are correct.
Dennis Zickefoose
+2  A: 

You could also add another level of categorization and sort pointers according to their "constness":

Pointer to a const - You can change to pointer, but not what it points to.

Example:

int i = 42;
const int* pi = &i;

or

int i = 42;
int const* pi = &i;

Const pointer - You can't change what the pointer points to.

Example:

int i = 42;
int* const pi = &i;

Const pointer to a const - You can't change what the pointer points to and you can't change the pointer itself.

Example:

int i = 42;
const int* const pi = &i;
waffleman