Why Do We Use Pointer Arrays In C++?
I have a lot of confusions about the use of pointers
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.
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
- Do While Loop In C++ Output?
- What Is File-based Approach?
- What Is Cascading Rollback?
- How Do I Create An SQL Queries That Show: Student Name, Address, And Contact Information For All Students? Student Name And The Certificate Program For Which They Are Registered Student Name And The Courses For Which They Are Registered?
- I Need To Create A System Where Students Can Register For One Of 5 Certificates, Select Courses, And Obtain A Transcript Of The Grades Received For The Courses. Each Student Can Be Enrolled For Only One Certificate. There Are 4 Courses In Each Cert?
- How The Efficiency And Integrity Of The Data Is Maintained At All Times Although The Database Is Used By Varying Organisational Levels? 2 Examples
- What Is An Attribute In Java?
- How About Links Of London?
- When You Create A New COM Component In Visual Basic, You Choose The Following Template?
- Why Do We Use Public Static Void Main In Java?
- What Parts Of Wordpad?
- What Do You Mean By Index?
- What Is Indexed File Organization?
- How The Performance Of Database?
- How Is That The Additional Hardware Affect The Data Base Management System?
- How Do You Automate Starting And Shutting Down Of Databases In Unix?
- How Design An Algorithm For The SIMD Computer To Calculate The Sum Of An Array (A) Of 8 Elements. Assume That Element Ai Is Stored In The Local Memory Of PEi, Where I=1,2,3,.......4?
- Do I Have To Write A Main Method In Every Class I Create?Why?
- Do I Have To Write A Main Method In Every Class I Create?
- What Is Centralize Database Management System?
- Why We Indicate FF As 0FF In Program?
- When To Apply For Css?
- How Do I Enable Java On My I465?
- What Are Key Features Of Procedural Programming?
- What Is Conceptual Methodology?
- What Are Arrays?
- What's Arrays?
- What Is Photovoltaic? And Photovoltaic Arrays Are Used?
- In Computer How We Use Arrays ?
- What Is An Arrays In C Language?
- Are There Any Usefil Tips On Arrays For C++ ?
- What Is The Difference Between Datastructures And Arrays?
- Does Java Support Multidimensional Arrays Or Not?
- What Is The Difference Between Arrays And Hash In Perl?
- What Multiplication Fact Can Be Found By Using The Arrays For 2 X 9 And 5 X 9?
- What Multiplication Fact Can Be Found By Using The Arrays For 2 X 9 And 5 X 9?

New Comment - Comments are editable for 5 min.