简体   繁体   English

“对象向量”的大小与“对象指针向量”的大小

[英]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? 输出是120和40。我可以理解40-因为它是10 * 4字节,但是对120感到惊讶。这是容器的实际大小-OCtr吗? It looks like sizeof(Test) is not the right way to get complete size of the object. 看起来sizeof(Test)不是获取对象完整大小的正确方法。

  • 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. 除了在Test类内部提供一个成员函数以汇总所有Test数据成员的内存外,还有另一个函数可以获取任何类对象的大小。
  • 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. 在您的情况下,它是12个字节。 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. 通过取向量类型的大小并乘以元素数,然后为适当的向量加上12个字节,可以计算出向量及其数据使用的内存总量。

sizeof is a compile-time operator. sizeof是一个编译sizeof算符。 That is, sizeof(X) will be replaced during compilation with a constant number. 也就是说,在编译过程中, sizeof(X)将被替换为一个常数。 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. 这意味着sizeofvector<T>总是相同sizeof一个vector<T>与成千上万其内部元件。 Remember, sizeof does not do anything during runtime, so the result cannot possibly reflect the current state of the vector. 请记住, sizeof在运行时不会执行任何操作,因此结果可能无法反映向量的当前状态。

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; 120个字节将是每个对象12个字节,每个对象仅存储一个向量,因此向量的大小与类的大小相同; 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. 如果我猜的话,我会猜一个int的大小,一个int的容量,以及另一个指向数据的指针,这些指针总共加起来为12。这是可以理解的。

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 . 请记住,向量将其数据存储在堆中,因此它不是本地数据,因此也不是由sizeof返回的vector对象的大小的一部分。 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. 因此, v.resize(2000)不会对对象的大小做任何事情,只会对向量正在使用的免费存储上的内存块大小v.resize(2000)

You are doing some things wrong. 您做错了一些事。 First, both OCtr and PCtr are the same type, which is vector of Test . 首先, OCtrPCtr是同一类型,这是Test向量。 Not of Test* as some of your code suggests. 并非某些代码建议的Test*

The expression PCtr.size()*sizeof(Test*) is basically meaningless because the things stored in PCtr aren't pointers but whole objects. 表达式PCtr.size()*sizeof(Test*)基本上是没有意义的,因为存储在PCtr中的不是指针,而是整个对象。 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). 如果它包含动态分配(如向量一样),我在C ++中没有一种理智的方法来获取对象的总大小。 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. 换句话说,当您有一个对象容器时,对sizeof(Test) .size()乘以.size()进行正确的操作,但是在尝试创建一个指针容器时,您需要格外小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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