C++ sizeof有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3203162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does sizeof do?
提问by dato datuashvili
What is the main function of sizeof
(I am new to C++). For instance
的主要功能是什么sizeof
(我是 C++ 新手)。例如
int k=7;
char t='Z';
What do sizeof (k)
or sizeof (int)
and sizeof (char)
mean?
dosizeof (k)
或sizeof (int)
andsizeof (char)
是什么意思?
回答by Peter Alexander
sizeof(x)
returns the amount of memory (in bytes) that the variable or type x
occupies. It has nothing to do with the value of the variable.
sizeof(x)
返回变量或类型x
占用的内存量(以字节为单位)。它与变量的值无关。
For example, if you have an array of some arbitrary type T
then the distance between elements of that array is exactly sizeof(T)
.
例如,如果您有一个任意类型的数组,T
那么该数组元素之间的距离正好是sizeof(T)
。
int a[10];
assert(&(a[0]) + sizeof(int) == &(a[1]));
When used on a variable, it is equivalent to using it on the type of that variable:
在变量上使用时,相当于在该变量的类型上使用它:
T x;
assert(sizeof(T) == sizeof(x));
As a rule-of-thumb, it is best to use the variable name where possible, just in case the type changes:
根据经验,最好尽可能使用变量名,以防类型发生变化:
int x;
std::cout << "x uses " << sizeof(x) << " bytes." << std::endl
// If x is changed to a char, then the statement doesn't need to be changed.
// If we used sizeof(int) instead, we would need to change 2 lines of code
// instead of one.
When used on user-defined types, sizeof
still returns the amount of memory used by instances of that type, but it's worth pointing out that this does not necessary equal the sum of its members.
当用于用户定义的类型时,sizeof
仍然返回该类型的实例使用的内存量,但值得指出的是,这不一定等于其成员的总和。
struct Foo { int a; char b; };
While sizeof(int) + sizeof(char)
is typically 5
, on many machines, sizeof(Foo)
may be 8
because the compiler needs to pad outthe structure so that it lies on 4 byte boundaries. This is not always the case, and it's quite possible that on your machine sizeof(Foo)
will be 5, but you can't depend on it.
虽然在许多机器上sizeof(int) + sizeof(char)
通常是5
,sizeof(Foo)
可能是8
因为编译器需要填充结构以使其位于 4 字节边界上。情况并非总是如此,很可能在您的机器上sizeof(Foo)
为 5,但您不能依赖它。
回答by Derrick Turk
To add to Peter Alexander's answer: sizeof yields the size of a value or type in multiples of the size of a char
---char
being defined as the smallest unit of memory addressable (by C or C++) for a given architecture (and, in C++ at least, at least 8 bits in size according to the standard). This is what's generally meant by "bytes" (smallest addressable unit for a given architecture) but it never hurts to clarify, and there are occasionally questions about the variability of sizeof (char)
, which is of course always 1
.
添加到 Peter Alexander 的回答中:sizeof 以 a 大小的倍数产生值或类型的大小char
---char
被定义为给定体系结构(并且,在 C++ 中)可寻址(通过 C 或 C++)的最小内存单元至少,根据标准大小至少为 8 位)。这就是“字节”(给定体系结构的最小可寻址单位)的通常含义,但澄清一下从来没有什么坏处,并且偶尔会有关于 的可变性的问题sizeof (char)
,当然总是1
。
回答by PeterK
sizeof() returns the size of the argument passed to it. sizeof() cpp reference
sizeof() 返回传递给它的参数的大小。 sizeof() cpp 参考
回答by Sina Raoufi
sizeof
is a compile time unary operator that returns size of data type.
For example:
sizeof
是一个编译时一元运算符,它返回数据类型的大小。例如:
sizeof(int)
will return the size of int
in byte.
将返回int
字节的大小。
Also remember that type sizes are platform dependent.
还要记住,类型大小取决于平台。
Check this page for more details: sizeof in C/C++
查看此页面以获取更多详细信息:C/C++ 中的 sizeof