I'm trying to understand how much memory is taken up by using a vector of objects versus vector of pointers. I wrote a small test code:
class Test {
public:
Test()
{ v.resize(2000); }
private:
std::vector<int> v;
};
int main()
{
std::vector<Test> OCtr;
std::vector<Test*> PCtr;
Octr.resize(10);
PCtr.resize(10, new Test);
cout << OCtr.size()*sizeof(Test) << endl;
cout << PCtr.size()*sizeof(Test*) << endl;
}
The output is 120 and 40. I can understand 40 - as it is 10* 4 bytes, but surprised by 120. Is that the real size of the container - OCtr? It looks like sizeof(Test) is not the right way to get complete size of the object.
- Instead of providing a member function inside Test class to sum up memory of all Test data members, is there another to get size of any class object.
- I have read answers on when to use vector of objects versus vector of object pointers. But in a case where I'm creating a container of simple class (no polymorphic behavior), where memory consumption is primary criteria and I can handle deletion of object pointers, which container should I choose?