C+ POINTERS

PREVIOUS

POINTERS
   
Q: What is a dangling pointer?
A: A dangling pointer arises when you use the address of an object after its lifetime is over. This
may occur in situations like returning addresses of the automatic variables from a function or
using the address of the memory block after it is freed.

Q: What is Memory Leak? A: Memory which has no pointer pointing to it and there is no way to delete or reuse this
memory(object), it causes Memory leak.
{
Base *b = new base();
}
Out of this scope b no longer exists, but the memory it was pointing to was not deleted. Pointer b
itself was destroyed when it went out of scope.

Q: What is auto pointer? A: The simplest example of a smart pointer is auto_ptr, which is included in the standard C++
library. Auto Pointer only takes care of Memory leak and does nothing about dangling pointers
issue. You can find it in the header . Here is part of auto_ptr's implementation, to illustrate what
it does:
template class auto_ptr
{
T* ptr;
public:
explicit auto_ptr(T* p = 0) : ptr(p) {}
~auto_ptr() {delete ptr;}
T& operator*() {return *ptr;}
T* operator->() {return ptr;}
// ...
};
As you can see, auto_ptr is a simple wrapper around a regular pointer. It forwards all meaningful
operations to this pointer (dereferencing and indirection). Its smartness in the destructor: the
destructor takes care of deleting the pointer.
For the user of auto_ptr, this means that instead of writing:
void foo()
{
MyClass* p(new MyClass);
p->DoSomething();
delete p;
}
You can write:
void foo()
{
auto_ptr p(new MyClass);
p->DoSomething();
}
And trust p to cleanup after itself.

Q: What issue do auto_ptr objects address? A: If you use auto_ptr objects you would not have to be concerned with heap objects not being
deleted even if the exception is thrown.

Q: What is a smart pointer?  A: A smart pointer is a C++ class that mimics a regular pointer in syntax and some semantics,
but it does more. Because smart pointers to different types of objects tend to have a lot of code in
common, almost all good-quality smart pointers in existence are templated by the pointee type,
as you can see in the following code:
template
class SmartPtr
{
public:
explicit SmartPtr(T* pointee) : pointee_(pointee);
SmartPtr& operator=(const SmartPtr& other);
~SmartPtr();
T& operator*() const
{
...
return *pointee_;
}
T* operator->() const
{
...
return pointee_;
}
private:
T* pointee_;
...
};
SmartPtr aggregates a pointer to T in its member variable pointee_. That's what most smart
pointers do. In some cases, a smart pointer might aggregate some handles to data and compute
the pointer on the fly.
The two operators give SmartPtr pointer-like syntax and semantics. That is, you can write
class Widget
{
public:
void Fun();
};
SmartPtr sp(new Widget);
sp->Fun();
(*sp).Fun();
Aside from the definition of sp, nothing reveals it as not being a pointer. This is the mantra of
smart pointers: You can replace pointer definitions with smart pointer definitions without
incurring major changes to your application's code. You thus get extra goodies with ease.
Minimizing code changes is very appealing and vital for getting large applications to use smart
pointers. As you will soon see, however, smart pointers are not a free lunch.

Q: Is there any problem with the following : char*a=NULL; char& p = *a;? A: The result is undefined. You should never do this. A reference must always refer to some
object.

Q: What is the difference between a pointer and a reference?A: A reference must always refer to some object and, therefore, must always be initialized;
pointers do not have such restrictions. A pointer can be reassigned to point to different objects
while a reference always refers to an object with which it was initialized.

Q: What is the difference between const char *myPointer and char *const myPointer?A: Const char *myPointer is a non constant pointer to constant data; while char *const
myPointer is a constant pointer to non constant data.

Q: When should I use references, and when should I use pointers?A: Use references when you can, and pointers when you have to.
References are usually preferred over pointers whenever you don't need "reseating". This usually
means that references are most useful in a class's public interface. References typically appear on
the skin of an object, and pointers on the inside.
The exception to the above is where a function's parameter or return value needs a "sentinel"
reference a reference that does not refer to an object. This is usually best done by
returning/taking a pointer, and giving the NULL pointer this special significance (references
should always alias objects, not a dereferenced NULL pointer).
Note: Old line C programmers sometimes don't like references since they provide reference
semantics that isn't explicit in the caller's code. After some C++ experience, however, one
quickly realizes this is a form of information hiding, which is an asset rather than a liability. E.g.,
programmers should write code in the language of the problem rather than the language of the
machine.

PREVIOUS

No comments:

Post a Comment