简体   繁体   中英

Size of “vector of objects” Versus “vector of Object pointers”

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?

If you take the size of a vector instance, it will always be the same for a particular system implementation. In your case it is 12 bytes. Vectors allocate memory using a default or supplied allocator for the actual storage of their object contents. This memory is by default from the heap and does not register in the size of the vector object itself.

You can calculate the total amount of memory used for the vector and its data by taking a sizeof the vector type and multiplying by the number of elements, then adding 12 bytes for the vector proper.

sizeof is a compile-time operator. That is, sizeof(X) will be replaced during compilation with a constant number. This means that the sizeof an empty vector<T> is always the same as the sizeof a vector<T> with thousands of elements inside of it. Remember, sizeof does not do anything during runtime, so the result cannot possibly reflect the current state of the vector.

120 bytes would be 12 bytes per object, where each object simply stores a vector, so the size of a vector and the size of your class are the same; if I take a guess, I'd guess an int for the size, and int for the capacity, and another pointer to the data, all adding up to 12. That's quite understandable.

Remember that vectors store their data in the heap, so it's not indigenous data, and therefore is not part of the size of the vector object which is returned by sizeof . Only the size of the pointer that points to that data is counted. So your v.resize(2000) doesn't do anything to the size of the object, only to the size of the block of memory on the free store the vector is using.

You are doing some things wrong. First, both OCtr and PCtr are the same type, which is vector of Test . Not of Test* as some of your code suggests.

The expression PCtr.size()*sizeof(Test*) is basically meaningless because the things stored in PCtr aren't pointers but whole objects. Yet you are computing the size of a pointer (which is always the same).

There is no sane way that I know of in C++ to get the total size of an object if it includes dynamic allocation (as vector does). In other words, you are doing the right thing to multiple .size() by sizeof(Test) when you have a container of objects, but you need to be more careful when you try to make a container of pointers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM