指针无法正确滚动浏览对象

问题描述 投票:0回答:2

我是一名大学生,我正在尝试解决教授给我的C ++练习。我的英语不好意思。简而言之,我必须不使用数组而是仅使用指针来管理一定数量(相同类型)的对象。有问题的对象是“房屋”类型的对象。

我创建了指向“房屋”的指针,并通过循环将指针指向了由用户输入的变量初始化的“房屋”类型的新对象。然后,我滑动指针并重新开始。

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);

}

问题出现在我滚动指针所指向的对象时。例如打印我创建的对象拥有的值。

先前的for循环使我指向了最后创建的对象旁边的位置的指针。因此,通过for循环,我将指针带回到第一个对象(因此使指针向后退四步),并且在每次迭代时,我让他打印指针所持有的内存地址,即每个House的内存地址。

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

这是最后一段代码的输出:

0x10139c

0x101390

0x101384

0x101378

第一个是与对象无关的地址,因为它是最后一个对象之后的位置所固有的地址。按照我的逻辑,后面的其他3个分别是第四,第三和第二宫的地址。

再次使用指针,对于每个对象,我都打印了它的值和地址

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;
}

这是输出:

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

我输入的初始输入是:

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

我不明白为什么它不能正确打印数据,以及由于什么原因在一次迭代中它打印对象的数据,而在下一次它打印随机数。

问题出在哪里?

c++ object pointers memory-address
2个回答
0
投票

这里,当您进行housePtr = new House(r,d);时,将创建新对象,并且housePtr指向该对象。现在,当您执行housePtr ++时,它将按House类的大小递增。现在再次执行housePtr = new House(r,d);时,将创建新对象,并且不一定是先前创建对象的连续地址。这是C ++中最重要的事情。 C ++为您提供了赤裸裸的记忆,无视安全性。蜘蛛侠说这是巨大的力量,“力量越大,责任越大”。在这里,您始终必须确保在动态创建对象时,始终存储指向该对象的指针。否则,您将永远无法获得该地址。这确实是一个巨大的问题,名为memory-leak

只是一个建议,我想您的教授希望您使用链接列表。如果使用链表,则不必创建某种指针数组来指向所有创建的对象。在链接到所有对象的皮棉地址中,会将其存储在先前的节点指针字段中。


0
投票

增加不是数组的指针将导致该指针指向未使用/无效的内存,这将导致未定义的行为(这就是为什么您得到奇怪的值)。每次也用new覆盖指针将导致内存泄漏。 Baiscally,您使用的指针就像是数组一样,即使不是。如果您始终必须制作4个House对象,请按照以下步骤操作:

House* housePtr1;
House* housePtr2;
House* housePtr3;
House* housePtr4;

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

int r, d;

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

housePtr1 = new House(r, d);



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

housePtr2 = new House(r, d);



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

housePtr3 = new House(r, d);



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

housePtr4 = new House(r, d);

然后打印值:

cout<<"\nThe house 1 has "<<housePtr1->getNumOfRooms()<<" rooms and is ";
cout<<housePtr1->getDistanceFromCenter()<<" meters from the center\n";
cout<<housePtr1<<endl;

cout<<"\nThe house 2 has "<<housePtr2->getNumOfRooms()<<" rooms and is ";
cout<<housePtr2->getDistanceFromCenter()<<" meters from the center\n";
cout<<housePtr2<<endl;

cout<<"\nThe house 3 has "<<housePtr3->getNumOfRooms()<<" rooms and is ";
cout<<housePtr3->getDistanceFromCenter()<<" meters from the center\n";
cout<<housePtr3<<endl;

cout<<"\nThe house 4 has "<<housePtr4->getNumOfRooms()<<" rooms and is ";
cout<<housePtr4->getDistanceFromCenter()<<" meters from the center\n";
cout<<housePtr4<<endl;

我猜您的教授想提出的观点是,在没有数组的情况下完成所有这些工作很痛苦;)

© www.soinside.com 2019 - 2024. All rights reserved.