The pointer does not scroll through objects properly

Ragnarox97 :

I'm a university student and I'm trying to solve a C++ exercise given to me by the professor. I'm sorry for my english. In a nutshell I have to manage a certain number of objects (of the same type) without using arrays but only using pointers. The objects in question are "House" type objects.

I create a pointer to "House" and with a loop I point the pointer to a new object of type "House" initialized with variables entered by the user. Then I slide the pointer and start again.

House* housePtr;

cout<<"We start building houses, you will have to build 4.\n";

for (auto i=0; i<4; ++i, ++housePtr)
{
    int r, d;

    cout<<"\nLet's build the number "<<i+1<<endl;
    cout<<"How many rooms must it have?\n";
    cin>>r;
    cout<<"\nHow far is it from the center?\n";
    cin>>d;

    housePtr= new House(r, d);

}

The problem comes when I scroll through the objects the pointer pointed to. For example to print the values ​​possessed by the objects I created.

The previous for loop leaves me the pointer to the position next to the last object created. So with a for loop I bring the pointer back to the first object (so I make it go 4 steps back) and at each iteration I make him print the memory address held by the pointer, that is, the memory address of each House.

for (auto i=0; i<4; i++, housePtr--)
{
    cout<<endl<<housePtr<<endl;
}

And this is the output of this last piece of code:

0x10139c

0x101390

0x101384

0x101378

The first is an address that has nothing to do with objects, because it is the one inherent in the position following the last object. The other 3 following are (according to my logic) the addresses of the fourth, third and second houses respectively.

Taking the pointer again, for each object I printed its values and also the address

for (auto i=0; i<4; housePtr++, i++)
{
    cout<<"\nThe house "<<i+1<<" has "<<housePtr->getNumOfRooms()<<" rooms and is ";
    cout<<housePtr->getDistanceFromCenter()<<" meters from the center\n";
    cout<<housePtr<<endl;
}

And this is the output:

The house 1 has 190607135 rooms and is 201338508 meters from the center
0x10136c

The house 2 has 7 rooms and is 4 meters from the center
0x101378

The house 3 has 190607135 rooms and is 201338508 meters from the center
0x101384

The house 4 has 5 rooms and is 8 meters from the center
0x101390

The initial inputs I entered are:

We start building houses, you will have to build 4.

Let's build the number 1
How many rooms must it have?
8

How far is it from the center?
7

Let's build the number 2
How many rooms must it have?
5

How far is it from the center?
8

Let's build the number 3
How many rooms must it have?
7

How far is it from the center?
4

Let's build the number 4
How many rooms must it have?
5

How far is it from the center?
8

I can't understand why it doesn't print the data correctly and for what reason at one iteration it prints the data of an object and at the next it prints random numbers.

Where is the problem?

Nishant Kathiriya :

Here when you do housePtr= new House(r, d); there will be new object created and housePtr will be pointing to it. Now when you do housePtr++ it will be incremented by size of House class. Now again when you do housePtr= new House(r, d); new object will be created and will not necessarily be consecutive address of the previously created object. This is the most important thing to take care about in C++. C++ provide you a naked memory with no regards to safety. Which is immense amount of power and as it is said by spider man "With great power, comes great responsibility". Here you will always have to ensure that when ever you crate a object dynamically, always store a pointer to that object. Otherwise you will not ever get that address back. And it is really huge problem named memory-leak.

Just a suggestion, I think you professor wants you to use linked-list. If you use linked-list then you will not have to create some kind of pointer array to point to all created objects. In linked lint address to all object will be stored in previous nodes pointer field.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=391254&siteId=1