如何通过引用获取对象的大小?

时间:2022-07-06 21:20:53

Suppose I have a class

假设我有一堂课

class Foo {

:
:

}

I have another function

我有另一个功能

void getf( Foo &f) {

:
:

std::cout<<sizeof f<<std::endl;
}

After I process the data and assign a lot of data to f (vector included in Foo members), I need the size of f object

在我处理数据并将大量数据分配给f(包含在Foo成员中的向量)之后,我需要f对象的大小

However, as what I did above, I always get 16, which is the size of a reference here.

但是,正如我上面所做的那样,我总是得到16,这是参考的大小。

Did I do anything wrong? How to do that?

我做错了什么吗?怎么做?

Thanks!

8 个解决方案

#1


8  

sizeof returns the size of a type. Often time, as in the case of a Vector, the size of the type does not necessarily include the cumulative size of everything the type may point to. For example, the type:

sizeof返回类型的大小。通常是时间,如Vector的情况,类型的大小不一定包括类型可能指向的所有内容的累积大小。例如,类型:

class Foo {
   char* chars
}

... will exhibit the same sizeof results whether chars points to a single byte or it points to a 40Kb string.

...将显示相同大小的结果,无论字符指向单个字节还是指向40Kb字符串。

#2


5  

According to this IBM reference, sizeof applied to a reference returns the size of the referenced object:

根据此IBM参考,应用于引用的sizeof返回引用对象的大小:

The result is the size of the referenced object.

结果是引用对象的大小。

So, I believe the problem is not that sizeof is returning the size of the reference itself, but instead that Foo holds pointers to other types.

所以,我认为问题不在于sizeof返回引用本身的大小,而是Foo持有指向其他类型的指针。

Also keep in mind that sizeof will not tell you the size of data inside a vector (or any other container that uses the heap). Consider the following example:

还要记住,sizeof不会告诉您向量(或使用堆的任何其他容器)内的数据大小。请考虑以下示例:

struct Foo {
    std::vector<int> v;
}

int main(int argc, char **argv) {
    Foo foo;
    std::cout << sizeof(foo) << std::endl;
    foo.v.push_back(1);
    std::cout << sizeof(foo) << std::endl;
}

Output:
24
24

#3


4  

After I process the data and assign a lot of data to f (vector included in Foo members), I need the size of f object

在我处理数据并将大量数据分配给f(包含在Foo成员中的向量)之后,我需要f对象的大小

sizeof is compile-time operation. It gives you the fixed size of the just variable or object itself. If your object has pointers to memory elsewhere (that's what vector uses internally), sizeof will never delve into them to determine the size of what they point to.

sizeof是编译时操作。它为您提供了固定大小的正变量或对象本身。如果你的对象在其他地方有指向内存的指针(这就是vector在内部使用的那个),那么sizeof将永远不会深入研究它们以确定它们所指向的大小。

If you need some measure of the total number of bytes associated with an object an runtime, then you can't just use a simple sizeof. You would have to add up all the pieces. For instance: sizeof(f) + f.vector_member.size() * sizeof(whaterver_type_that_vector_contains).

如果您需要测量与运行时对象关联的总字节数,那么您不能只使用简单的sizeof。你必须把所有的东西都加起来。例如:sizeof(f)+ f.vector_member.size()* sizeof(whaterver_type_that_vector_contains)。

#4


1  

sizeof in this case is returning the size of your class. You say you assigned data to Foo, but you didn't define Foo. I suspect it looks somethign like this:

在这种情况下,sizeof返回您的类的大小。你说你把数据分配给了Foo,但你没有定义Foo。我怀疑它看起来像这样:

class Foo
{
public:
  string my_str;
  int* my_ints;
};

And then you do something like this:

然后你做这样的事情:

Foo f;
f.my_str = "Hello, Foo.";
f.my_ints = new int[1000];

...and expect that sizeof(f) will now be the size of Foo, plus the size of the string, plus the size of the int array. But it's not. Foo never changes in size because it doesn't "have" a char array or an int array. Rather it has a string object (which in turn probably has a pointer to a char array), and a pointer to int. The memory allocated for the string and the int array isn't "in" Foo, it is pointed to by Foo.

...并且期望sizeof(f)现在将是Foo的大小,加上字符串的大小,加上int数组的大小。但事实并非如此。 Foo从不会改变大小,因为它没有“拥有”char数组或int数组。相反,它有一个字符串对象(反过来可能有一个指向char数组的指针),以及一个指向int的指针。为字符串和int数组分配的内存不是“在”Foo中,它由Foo指向。

#5


0  

Can you list the structure of your class F. I am suspecting that you are getting the right size, sizeof is going to tell you the size of the class, which is the size of all members of the class (this is based on the alignment and ordering as well as your compiler). It will not tell you the size of the vector including its elements, just the size of the reference to the vector.

你能列出你的类F的结构吗?我怀疑你得到了正确的大小,sizeof会告诉你类的大小,这是类的所有成员的大小(这是基于对齐和订购以及您的编译器)。它不会告诉你矢量的大小,包括它的元素,只是向量的引用大小。

#6


0  

Using this test case:

使用此测试用例:

class Foo 
{ 
    char x [100];  
} ;

void getf( Foo &f) 
{ 
    std::cout<<"sizeof f:" << sizeof f<<std::endl; 
} 

I am getting sizeof f:100 printed. So, you code is correct; you are just interpreting it wrong.

我的尺寸为f:100印。所以,你的代码是正确的;你只是在解释错误。

#7


0  

If you have a class like this:

如果您有这样的课程:

class Foo
{
    std::vector<int> intVec;

    // ....
};

the sizeof operator, which works at compile-time, will only include the size of the vector's instance data members. It will not give you the total size of the elements of the vector, which are stored on the heap somewhere and pointed to by some private member of the vector.

在编译时工作的sizeof运算符只包含向量的实例数据成员的大小。它不会给你向量元素的总大小,它们存储在某个地方的堆上,并由向量的某个私有成员指向。

If you are really trying to figure out the total amount of memory used by the vector, I don't think there is a portable way to do that. The vector implementation may allocate memory it is not using, so without looking at the implementation, you don't really know what it's doing.

如果你真的想弄清楚向量使用的内存总量,我认为没有一种可移植的方法可以做到这一点。向量实现可能会分配它不使用的内存,因此如果不查看实现,您实际上并不知道它在做什么。

#8


0  

Just an additional note:

另外一个注意事项:

$8.3.2/3 states- "It is unspecified whether or not a reference requires storage (3.7)."

$ 8.3.2 / 3表示 - “未指明参考文献是否需要存储(3.7)。”

Therefore one should not always assume that reference occupies storage. sizeof when applied to a reference operand, returns the static compile time sizeof of the original referand.

因此,不应总是假设参考占用存储空间。 sizeof应用于引用操作数时,返回原始referand的静态编译时sizeof。

#1


8  

sizeof returns the size of a type. Often time, as in the case of a Vector, the size of the type does not necessarily include the cumulative size of everything the type may point to. For example, the type:

sizeof返回类型的大小。通常是时间,如Vector的情况,类型的大小不一定包括类型可能指向的所有内容的累积大小。例如,类型:

class Foo {
   char* chars
}

... will exhibit the same sizeof results whether chars points to a single byte or it points to a 40Kb string.

...将显示相同大小的结果,无论字符指向单个字节还是指向40Kb字符串。

#2


5  

According to this IBM reference, sizeof applied to a reference returns the size of the referenced object:

根据此IBM参考,应用于引用的sizeof返回引用对象的大小:

The result is the size of the referenced object.

结果是引用对象的大小。

So, I believe the problem is not that sizeof is returning the size of the reference itself, but instead that Foo holds pointers to other types.

所以,我认为问题不在于sizeof返回引用本身的大小,而是Foo持有指向其他类型的指针。

Also keep in mind that sizeof will not tell you the size of data inside a vector (or any other container that uses the heap). Consider the following example:

还要记住,sizeof不会告诉您向量(或使用堆的任何其他容器)内的数据大小。请考虑以下示例:

struct Foo {
    std::vector<int> v;
}

int main(int argc, char **argv) {
    Foo foo;
    std::cout << sizeof(foo) << std::endl;
    foo.v.push_back(1);
    std::cout << sizeof(foo) << std::endl;
}

Output:
24
24

#3


4  

After I process the data and assign a lot of data to f (vector included in Foo members), I need the size of f object

在我处理数据并将大量数据分配给f(包含在Foo成员中的向量)之后,我需要f对象的大小

sizeof is compile-time operation. It gives you the fixed size of the just variable or object itself. If your object has pointers to memory elsewhere (that's what vector uses internally), sizeof will never delve into them to determine the size of what they point to.

sizeof是编译时操作。它为您提供了固定大小的正变量或对象本身。如果你的对象在其他地方有指向内存的指针(这就是vector在内部使用的那个),那么sizeof将永远不会深入研究它们以确定它们所指向的大小。

If you need some measure of the total number of bytes associated with an object an runtime, then you can't just use a simple sizeof. You would have to add up all the pieces. For instance: sizeof(f) + f.vector_member.size() * sizeof(whaterver_type_that_vector_contains).

如果您需要测量与运行时对象关联的总字节数,那么您不能只使用简单的sizeof。你必须把所有的东西都加起来。例如:sizeof(f)+ f.vector_member.size()* sizeof(whaterver_type_that_vector_contains)。

#4


1  

sizeof in this case is returning the size of your class. You say you assigned data to Foo, but you didn't define Foo. I suspect it looks somethign like this:

在这种情况下,sizeof返回您的类的大小。你说你把数据分配给了Foo,但你没有定义Foo。我怀疑它看起来像这样:

class Foo
{
public:
  string my_str;
  int* my_ints;
};

And then you do something like this:

然后你做这样的事情:

Foo f;
f.my_str = "Hello, Foo.";
f.my_ints = new int[1000];

...and expect that sizeof(f) will now be the size of Foo, plus the size of the string, plus the size of the int array. But it's not. Foo never changes in size because it doesn't "have" a char array or an int array. Rather it has a string object (which in turn probably has a pointer to a char array), and a pointer to int. The memory allocated for the string and the int array isn't "in" Foo, it is pointed to by Foo.

...并且期望sizeof(f)现在将是Foo的大小,加上字符串的大小,加上int数组的大小。但事实并非如此。 Foo从不会改变大小,因为它没有“拥有”char数组或int数组。相反,它有一个字符串对象(反过来可能有一个指向char数组的指针),以及一个指向int的指针。为字符串和int数组分配的内存不是“在”Foo中,它由Foo指向。

#5


0  

Can you list the structure of your class F. I am suspecting that you are getting the right size, sizeof is going to tell you the size of the class, which is the size of all members of the class (this is based on the alignment and ordering as well as your compiler). It will not tell you the size of the vector including its elements, just the size of the reference to the vector.

你能列出你的类F的结构吗?我怀疑你得到了正确的大小,sizeof会告诉你类的大小,这是类的所有成员的大小(这是基于对齐和订购以及您的编译器)。它不会告诉你矢量的大小,包括它的元素,只是向量的引用大小。

#6


0  

Using this test case:

使用此测试用例:

class Foo 
{ 
    char x [100];  
} ;

void getf( Foo &f) 
{ 
    std::cout<<"sizeof f:" << sizeof f<<std::endl; 
} 

I am getting sizeof f:100 printed. So, you code is correct; you are just interpreting it wrong.

我的尺寸为f:100印。所以,你的代码是正确的;你只是在解释错误。

#7


0  

If you have a class like this:

如果您有这样的课程:

class Foo
{
    std::vector<int> intVec;

    // ....
};

the sizeof operator, which works at compile-time, will only include the size of the vector's instance data members. It will not give you the total size of the elements of the vector, which are stored on the heap somewhere and pointed to by some private member of the vector.

在编译时工作的sizeof运算符只包含向量的实例数据成员的大小。它不会给你向量元素的总大小,它们存储在某个地方的堆上,并由向量的某个私有成员指向。

If you are really trying to figure out the total amount of memory used by the vector, I don't think there is a portable way to do that. The vector implementation may allocate memory it is not using, so without looking at the implementation, you don't really know what it's doing.

如果你真的想弄清楚向量使用的内存总量,我认为没有一种可移植的方法可以做到这一点。向量实现可能会分配它不使用的内存,因此如果不查看实现,您实际上并不知道它在做什么。

#8


0  

Just an additional note:

另外一个注意事项:

$8.3.2/3 states- "It is unspecified whether or not a reference requires storage (3.7)."

$ 8.3.2 / 3表示 - “未指明参考文献是否需要存储(3.7)。”

Therefore one should not always assume that reference occupies storage. sizeof when applied to a reference operand, returns the static compile time sizeof of the original referand.

因此,不应总是假设参考占用存储空间。 sizeof应用于引用操作数时,返回原始referand的静态编译时sizeof。