指针无法正确滚动对象
The pointer does not scroll through objects properly
我是一名大学生,我正在尝试解决教授给我的 C++ 练习。我为我的英语感到抱歉。简而言之,我必须在不使用数组但仅使用指针的情况下管理一定数量的对象(相同类型)。有问题的对象是 "House" 类型的对象。
我创建了一个指向 "House" 的指针,并通过一个循环将指针指向了一个 "House" 类型的新对象,该对象使用用户输入的变量进行了初始化。然后我滑动指针重新开始。
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 循环,我将指针带回第一个对象(因此我让它向后退了 4 步),并且在每次迭代中,我都让他打印指针持有的内存地址,即每个房屋的内存地址。
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
我不明白为什么它不能正确打印数据,以及为什么它在一次迭代中打印对象的数据,而在下一次迭代中打印随机数。
问题出在哪里?
这里当你做 housePtr= new House(r, d); 会创建新的对象并且 housePtr 将指向它。现在,当您执行 housePtr++ 时,它会增加房屋的大小 class。现在再次当你做 housePtr= new House(r, d); 新对象将被创建并且不一定是以前的连续地址创建的对象。
这是在 C++ 中需要注意的最重要的事情。 C++ 为您提供不考虑安全性的裸内存。这是巨大的力量,正如蜘蛛侠所说 "With great power, comes great responsibility"。
在这里,您将始终必须确保在动态创建对象时始终存储指向该对象的指针。否则您将永远无法取回该地址。这确实是一个名为 memory-leak.
的大问题
只是一个建议,我想你教授希望你使用链表。如果您使用链表,那么您将不必创建某种指针数组来指向所有已创建的对象。在链接的 lint 中,所有对象的地址都将存储在先前的节点指针字段中。
递增一个不是数组的指针会导致该指针指向unused/invalid 内存,这会导致未定义的行为(这就是您得到奇怪值的原因)。每次用 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;
我猜你的教授想表达的意思是,在没有数组的情况下做这一切是一件痛苦的事;)
我是一名大学生,我正在尝试解决教授给我的 C++ 练习。我为我的英语感到抱歉。简而言之,我必须在不使用数组但仅使用指针的情况下管理一定数量的对象(相同类型)。有问题的对象是 "House" 类型的对象。
我创建了一个指向 "House" 的指针,并通过一个循环将指针指向了一个 "House" 类型的新对象,该对象使用用户输入的变量进行了初始化。然后我滑动指针重新开始。
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 循环,我将指针带回第一个对象(因此我让它向后退了 4 步),并且在每次迭代中,我都让他打印指针持有的内存地址,即每个房屋的内存地址。
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
我不明白为什么它不能正确打印数据,以及为什么它在一次迭代中打印对象的数据,而在下一次迭代中打印随机数。
问题出在哪里?
这里当你做 housePtr= new House(r, d); 会创建新的对象并且 housePtr 将指向它。现在,当您执行 housePtr++ 时,它会增加房屋的大小 class。现在再次当你做 housePtr= new House(r, d); 新对象将被创建并且不一定是以前的连续地址创建的对象。 这是在 C++ 中需要注意的最重要的事情。 C++ 为您提供不考虑安全性的裸内存。这是巨大的力量,正如蜘蛛侠所说 "With great power, comes great responsibility"。 在这里,您将始终必须确保在动态创建对象时始终存储指向该对象的指针。否则您将永远无法取回该地址。这确实是一个名为 memory-leak.
的大问题只是一个建议,我想你教授希望你使用链表。如果您使用链表,那么您将不必创建某种指针数组来指向所有已创建的对象。在链接的 lint 中,所有对象的地址都将存储在先前的节点指针字段中。
递增一个不是数组的指针会导致该指针指向unused/invalid 内存,这会导致未定义的行为(这就是您得到奇怪值的原因)。每次用 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;
我猜你的教授想表达的意思是,在没有数组的情况下做这一切是一件痛苦的事;)