Home TechnologyProgramming Subscribe to RSS

Why Do We Use Pointer Arrays In C++?

I have a lot of confusions about the use of pointers

Answer Question

1 Answer - Sort by: Date | Rating

    Arrays in C++ are stored on the stack. But stack has less memory to fulfill all the needs of running memory especially in the multiprogramming systems there is severe need to use different techniques to optimize memory use. Arrays of pointer provide an easy way for this purpose.  It reduces the burden on stack. Consider the following code of C++ that illustrates the use of arrays of pointers.

    An array of pointers to objects
    #include
         class point
         {
            public:
               point() { x=0;   y=0; }   
               ~point() {}                                 // destructor
               int Getx() const { return x; }
               int Gety() const { return y; }
               void Setx(int x) { x=x; }

           private:
              int x;
              int y;
        };

        int main()
        {
           point * line[500];
           int i;
           point * ppoint;
           for (i = 0; i < 500; i++)
           {
              ppoint = new point;
              ppoint->Setx(i);
              ppoint->Sety(i);
              line[i] = ppoint;
        }
         return 0;
    }

    The code defines a class point that has two data members, x and y. In order to use this class we need to create an object. This code uses point objects to calculate a line object as line is a collection of points that are between its endpoints. In normal situation i.e., without the use of pointer arrays all these 500 point objects have to be stored in the stack but through the use of pointer stack space is saved.
    0 0

    Raaga  

    answered 3 years ago

      More

         
         

        Ask a Question via Twitter

        Send a question to @askblurtit and we will publish it online and send you a reply everytime you receive an answer.

        Blurtit Store

        Get T-shirts, hoodies, caps and more at the Blurtit store

        Blurtit International