C++

Topic: Pointers

What are problems with pointers ?

What are the common problems we face in C++ programs while using pointers? The answer is memory management. Have a look at the following code:char* pName  = new char[1024];…SetName(pName);……if(null != pName){       delete[] pName; }How many times have we found a bug which was caused because we forgot to delete pName. It would be great if someone could take care of releasing the memory when the pointer is not useful (we are not talking about the garbage collector here). What if the pointer itself takes care of that? Yes, that’s exactly what smart pointers are intended to do. Let us write a smart pointer and see how we can handle a pointer better.We shall start with a realistic example. Let’s say we have a class called Person which is defined as below.class Person{    int age;    char* pName;    public:        Person(): pName(0),age(0)        {        }        Person(char* pName, int age): pName(pName), age(age)        {        }        ~Person()        {        }        void Display()        {            printf("Name = %s Age = %d \n", pName, age);        }        void Shout()        {            printf("Ooooooooooooooooo",);        } };Now we shall write the client code to use Person.Collapse | Copy Codevoid main(){    Person* pPerson  = new Person("Scott", 25);    pPerson->Display();    delete pPerson;}Now look at this code, every time I create a pointer, I need to take care of deleting it. This is exactly what I want to avoid. I need some automatic mechanism which deletes the pointer. One thing which strikes to me is a destructor. But pointers do not have destructors, so what? Our smart pointer can have one. So we will create a class called SP which can hold a pointer to the Person class and will delete the pointer when its destructor is called. Hence my client code will change to something like this:void main(){    SP p(new Person("Scott", 25));    p->Display();    // Dont need to delete Person pointer..}Note the following things:    We have created an object of class SP which holds our Person class pointer. Since the destructor of the SP class will be called when this object goes out of scope, it will delete the Person class pointer (as its main responsibility); hence we don’t have the pain of deleting the pointer.    One more thing of major importance is that we should be able to call the Display method using the SP class object the way we used to call using the Person class pointer, i.e., the class should behave exactly like a pointer

Browse random answers:

What is Pointers in c++ ?
What is importance of const. pointer in copy constructor?
Explain "passing by value", "passing by pointer" and "passing by reference"
Where we use reference and where we use pointers ?Write their differences and advantages and disadvantages .
Explain which of the following declarations will compile and what will be constant - a pointer or the value pointed at: * const char ** char const ** char * const
Why Pointers are not used in C Language. What are the main differences between C and C++
Explain about Reference operator (&) ?
Explain about declaring variables of pointer types with example ?
Explain about pointers and arrays ?
Explain about Pointer initialization ?
Explain about Pointer arithmetics ?
Explain about Pointers to pointers ?
Explain the void pointers with example ?
What is Null pointer ? 
What is Pointers to functions ?
What Are Pointers?
What are Using Pointers in C++ ?
Explain Accessing the Value at the Memory Address held by a Pointer ?
Why Use Pointers?
What are C++ Pointer Syntax ?
Explain about Taking Stock of Pointers ?
What are pointers? Why should you care?
Explain about Manipulating Data with Pointers and example ?
What are Array of pointers ?
Where do we use pointers?
Why do we need pointers?
Allocating a memory from the "Free Store" ?
Explain about Allocating a memory with new operator ?
Explain about De-allocating a memory with delete operator ?
Explain about Data type class pointers with example ?
Explain about memory leaks associated with pointers ?
Explain about memory leak caused by pointer reassignment ?
When local variable’s name is same as member’s name
What are function Pointer Syntax ?
What are Reading Function Pointer Declarations ?
What are Using a Function Pointer ?
What are Function Pointers in the Wild ?
How to Using Polymorphism and Virtual Functions Instead of Function Pointers (C++) ?
What are Benefits of Function Pointers ?
Where They get Useful  Function Arguments ?
Explain Smart Pointers (Modern C++) ?
What are Uses for smart pointers ?
What are Kinds of Smart Pointers ?
What are another Use of Pointers Efficient Array Processing ?
Explain about C++ Pointers and new
What are problems with pointers ?
Explain about generic smart pointer class ?
How to initialize a vector of pointers?
Explain about c++ Pointer Arithmetic ?
Explain about incrementing a Pointer ?
Explain  decrementing a Pointer ?
Explain pointer Comparisons ?