C ++中的sizeof()运算符是做什么的 [英] what is sizeof() operator doing in C++
问题描述
C中的 sizeof()
运算符在编译时给出其操作数的大小.它不评估其操作数.例如
The sizeof()
operator in C gives the size of its operand at compile time. It does not evaluate its operand. For example,
int ar1[10];
sizeof(ar1) // output 40=10*4
sizeof(ar1[-1]) // output 4
int ar2[ sizeof(ar1) ]; // generate an array of 40 ints.
谈到C ++模板类时,我发现了一些奇怪的结果.
When it came to C++ template class, I find some strange result.
template<typename T>
struct S{
T a;
};
sizeof( S<int> ) // output 4
sizeof( S<bool> ) // output 1
sizeof( vector<int> ) // output 24
sizeof( vector<char> ) // output 24
sizeof( vector<bool> ) // output 40
我猜vector或其他STL容器上的 sizeof
取决于特定的环境.
I guess the sizeof
on vector or other STL container depends on specific environment.
问题1.如何在C/C ++中实现 sizeof
?它不能是运行时函数.是宏吗?(我在在线教学视频中学到了什么).如果它是一个宏,它的 #define
是什么样的?什么时候执行 sizeof()
?
Question 1. How is sizeof
implemented in C/C++? It cannot be a run-time function. Is it a macro? (what I learned in a online tutorial vedio). If it is a Macro, what the #define
of it looks like? When the sizeof()
is executed?
问题2.如果我在 struct S
的定义中添加成员方法 void f(){}
. sizeof(S< int>)
仍为4.结构体的大小不应该增加吗?
Question 2. If I add a member method void f(){}
to the definition of struct S
. The sizeof(S<int>)
is still 4. Shouldn't the size of the struct increase?
问题3..STL容器是模板类.以 vector
为例,它具有12个成员属性/类型和许多成员方法?容易解释 sizeof(S< int>)
的输出.但是我发现很难解释 sizeof(vector< int>)
的输出.模板类应在编译时实例化,并且编译器应完全了解该类的大小,即 vector< int>
.所以 sizeof()
操作员应该知道.
Question 3. STL containers are template classes. Take vector
for example, it has 12 member attributes/types and many member methods? It is easy to explain the output of sizeof( S<int> )
. But I find it hard to explain the output of sizeof( vector<int> )
. Template class should be instantiated at compile-time and the compiler should have total knowledge of the size of the class, i.e. vector<int>
. So should sizeof()
operator know.
推荐答案
按照问题1 : sizeof
由编译器实现和评估.它不是宏,并且始终提供编译时结果.从概念上讲,您可以想象编译器用数字替换每个 sizeof
.
As per Question 1: sizeof
is implemented and evaluated by the compiler. It is not a macro, and it always provides a compile-time result. Conceptually, you can imagine that the compiler replaces every sizeof
with a number.
根据问题2 : sizeof
计算一个 S
实例占用的存储量.方法不按实例存储,而仅按字段存储(因为每个实例存在一次).但是,一种方法确实会占用某个位置的静态存储空间,以保存该功能的机器代码.
As per Question 2: sizeof
counts the amount of storage that one instance of S
occupies. A method does not take per-instance storage, only fields do (since they exist once per instance). A method does, however, occupy static storage somewhere to hold the machine code for the function.
根据问题3 :对于 sizeof(vector< int>)
,编译器将计算 vector< int>
的大小为此实例化.如果由于 vector
可以具有可变大小而感到困惑:的确如此,但是多余的存储空间是从堆中分配的,因此不会反映在 sizeof
的应用结果中到 vector
.
As per Question 3: For sizeof(vector<int>)
the compiler computes the size of the vector<int>
, which it instantiates in order to do so. If you are confused because a vector
can be of variable size: that is true, but the extra storage is allocate from the heap and thus not reflected in the result of sizeof
applied to a vector
.
这篇关于C ++中的sizeof()运算符是做什么的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!