Why Do We Use Pointer Arrays In C++?

1 Answers

raaga Profile
raaga answered
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.

Answer Question