Reputation: 2989
struct struct1
{
vector<unsigned> vec1;
};
void main()
{
vector<struct1> vecStruct1;
struct1 obj1Struct1;
struct1 obj2Struct1;
vecStruct1.push_back(obj1Struct1);
vecStruct1.push_back(obj2Struct1);
}
Now if I resize vec1
of struct1
of object vecStruct1[0]
, then is it also the case that vecStruct1
also gets resized i.e. will it cause reallocation in vecStruct1
i.e. will &vecStruct[0]
and &vecStruct[1]
not be different after resizing vec1
of vecStruct1[0]
?
Upvotes: 0
Views: 300
Reputation: 5726
Maybe this example will clarify the situation:
#include <iostream>
#include <vector>
using namespace std;
struct struct1
{
vector<unsigned> vec1;
};
int main() {
vector<struct1> vecStruct1;
struct1 obj1Struct1;
struct1 obj2Struct1;
vecStruct1.push_back(obj1Struct1);
vecStruct1.push_back(obj2Struct1);
cout << "Before resizing the vector:" << endl;
cout << "Address of vecStruct1[0]:" << &vecStruct1[0] << endl;
cout << "Address of vecStruct1[1]:" << &vecStruct1[1] << endl;
cout << "Capacity of the vector in vecStruct1[0]:" << vecStruct1[0].vec1.capacity() << endl;
cout << "Capacity of the vector in vecStruct1[1]:" << vecStruct1[1].vec1.capacity() << endl;
cout << endl;
vecStruct1[0].vec1.resize(1000);
cout << "After resizing vecStruct1[0]:" << endl;
cout << "Address of vecStruct1[0]:" << &vecStruct1[0] << endl;
cout << "Address of vecStruct1[1]:" << &vecStruct1[1] << endl;
cout << "Capacity of the vector in vecStruct1[0]:" << vecStruct1[0].vec1.capacity() << endl;
cout << "Capacity of the vector in vecStruct1[1]:" << vecStruct1[1].vec1.capacity() << endl;
cout << endl;
vecStruct1[1].vec1.resize(2000);
cout << "After resizing vecStruct1[1]:" << endl;
cout << "Address of vecStruct1[0]:" << &vecStruct1[0] << endl;
cout << "Address of vecStruct1[1]:" << &vecStruct1[1] << endl;
cout << "Capacity of the vector in vecStruct1[0]:" << vecStruct1[0].vec1.capacity() << endl;
cout << "Capacity of the vector in vecStruct1[1]:" << vecStruct1[1].vec1.capacity() << endl;
cout << endl;
return 0;
}
The output (you can check it on ideone: http://ideone.com/JVaiqg) is:
Before resizing the vector:
Address of vecStruct1[0]:0x8f33018
Address of vecStruct1[1]:0x8f33024
Capacity of the vector in vecStruct1[0]:0
Capacity of the vector in vecStruct1[1]:0
After resizing vecStruct1[0]:
Address of vecStruct1[0]:0x8f33018
Address of vecStruct1[1]:0x8f33024
Capacity of the vector in vecStruct1[0]:1000
Capacity of the vector in vecStruct1[1]:0
After resizing vecStruct1[1]:
Address of vecStruct1[0]:0x8f33018
Address of vecStruct1[1]:0x8f33024
Capacity of the vector in vecStruct1[0]:1000
Capacity of the vector in vecStruct1[1]:2000
And as you can see, the address of vecStruct1[0]
and vecStruct1[1]
never changes. That's because each element of the vector has a fixed size, and internally it contains a pointer to the area where the resized vectors are stored. When you resize the vectors (the "internal" ones, that is, each vec1
inside each struct1
) it's possible that they have to be moved to a new address, but the "external" vector vecStruct1
stays where it is.
Upvotes: 2
Reputation: 409482
No, the two vectors are totally unrelated. Adding elements to one vector, or resizing it some other way, will not cause any other vector to become resized.
Also, resizing vecStruct1[0].vec1
will not resize vecStruct1[1].vec1
.
Clarifications:
If you have a pointer to the first element in vecStruct1
(i.e. &vecStruct1[0]
) that pointer will be invalidated if you resize vecStruct1
. What you do with vecStruct1[0].vec1
doesn't matter here, and pointer to element in vecStruct1[0].vec1
will not change.
And if you have a pointer &(vecStruct1[0].vec1[0])
(parentheses not needed but added for clarity) then if you resize vecStruct1
that pointer will not change. You need to resize vecStruct1[0].vec1
for that pointer to become invalid.
Upvotes: 1
Reputation: 1974
The vector handles its memory outside of the structure, in the heap as mentioned in another answer. So resizing or reallocating the vector inside the structure won't change the structure's size at all, not even the vector containing that structure.
http://www.cplusplus.com/reference/vector/vector/
The vector handles its own memory dynamically ( in the heap ).
Upvotes: 0
Reputation: 13318
Nope, vector
stores its element on the heap not inside itself in the structure, so the vector
per se never grows when you resize it, what grows is the data buffer pointed by vector
.
Upvotes: 3
Reputation: 218323
As vecStruct1
contain copy of obj1Struct1
, once the object is added, the 2 object are distinct.
so modify obj1Struct1
doesn't modify vecStruct1[0]
.
Upvotes: 2