给定该类的对象数组,类成员向量的内存分配有问题吗?
我的基本程序结构是这样的:
class B1
{
vector <someStruct> aStruct; //some struct contains a vector of 'someotherStruct's, etc
B1() { cout << &aStruct << " ";};
};
class B2 {B1* pB1;};
class A
{
B1 object_B1;
B2 object_B2;
A() { objectB2.pB1 = &object_B1;};
};
int main()
{
vector <A> someA;
for(int q=0; q < N; q++)
someA.push_back(A());
cout << endl;
for (int q=0; q < N; q++)
cout << someA[q].B2.pB1 << " ";
}
所以如果 N 是 5,输出将是: 0xbffff828 0xbffff828 0xbffff828 0xbffff828 0xbffff828 \n
0xbffff828 0xbffff828 0xbffff828 0xbffff828 0xbffff828
换句话说,每个对象的向量在内存中占用相同的空间。程序输出也证明了这一点,因为通过不同对象访问向量中的数据会给出相同的值,即使它们应该不同。当然,另一个奇怪的事情是它为我提供了与对象相同的向量地址。
My basic program structure is this:
class B1
{
vector <someStruct> aStruct; //some struct contains a vector of 'someotherStruct's, etc
B1() { cout << &aStruct << " ";};
};
class B2 {B1* pB1;};
class A
{
B1 object_B1;
B2 object_B2;
A() { objectB2.pB1 = &object_B1;};
};
int main()
{
vector <A> someA;
for(int q=0; q < N; q++)
someA.push_back(A());
cout << endl;
for (int q=0; q < N; q++)
cout << someA[q].B2.pB1 << " ";
}
So if N was 5, the output would be:
0xbffff828 0xbffff828 0xbffff828 0xbffff828 0xbffff828 \n
0xbffff828 0xbffff828 0xbffff828 0xbffff828 0xbffff828
In other words, each object's vector occupies the same space in memory. And program output bears this out as well, since accessing data in the vector through different objects gives the same values, even when they should be different. The other weird thing, of course, is it gives me the same address for the vector as for the object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
忽略程序中的大量错误...
这将创建一个
A
类型的临时对象。object_B2.pB1
成员变量被初始化为指向该临时对象的object_B1
成员变量。然后,这个临时
A
对象将复制到someA
容器中。此副本的object_B1
成员变量的地址不同,但您无需更新object_B2.pB1
成员变量以指向新实例。为此,您需要实现一个复制构造函数(并且,为了正确性,您还需要实现一个复制赋值运算符)。所有指针最终都相同的原因是临时对象都是在内存中的同一位置创建的,这使得因为您可能在循环中调用
push_back
,所以可以在每次循环迭代时在堆栈上的同一位置创建临时对象。Ignoring the large number of errors in the program...
This creates a temporary object of type
A
. Theobject_B2.pB1
member variable is initialized to point to theobject_B1
member variable of this temporary object.This temporary
A
object is then copied into thesomeA
container. The address of this copy'sobject_B1
member variable is different, but you don't update theobject_B2.pB1
member variable to point into the new instance. To do this, you would need to implement a copy constructor (and, for correctness, you would also need to implement a copy assignment operator).The reason that all of the pointers end up being the same is that the temporary objects are all created in the same location in memory, which makes since, since you are presumably calling
push_back
in a loop, so the temporary object can be created on the stack in the same place at each iteration through the loop.您缺少 A 的复制因子,并且默认复制语义将指针值从一个 B2 对象复制到另一个对象。结构中的每个项目都看到相同的地址,因为它们都是从同一源对象复制的。当这个指向的对象稍后被销毁时,您将可以让 UB 访问它。
解决方法:
您还需要一个复制赋值运算符,因为默认的复制赋值运算符也有类似的问题。如果您希望禁用此 op=,请将其声明为私有而不定义它。
You are missing a copy ctor for A and the default copy semantics copy the pointer value from one B2 object to another. Each item in the struct sees the same address because they are all copied from the same source object. When this pointed-to object is later destroyed, you'll have UB accessing it.
The fix:
You also need a copy-assignment operator because the default one has a similar problem. Declare this op= as private without defining it if you want it disabled.