Originally, this is a part of another question.
Why is sizeof
called a compile-time operator? Isn't it actually a run-time operator? And if it is indeed a compile-time operator, how does it help in producing portable code which runs the same in different computers ? Please explain in detail.
4 Answers 4
sizeof()
gives you the size of the data type, not the size of a particular instance of that type in memory.
For example, if you had a string data object that allocated a variable size character array at runtime, sizeof()
could not be used to determine the size of that character array. It would only give you the size of the pointer.
The size of a data type is always known at compile time.
-
3Because C(++) doesn't have run-time object metadata you can't really get those things at run time either.C. Ross– C. Ross2013年04月18日 17:28:41 +00:00Commented Apr 18, 2013 at 17:28
-
6Actually, if you use
sizeof
on an array, you will get the size of the array (that is the element size times the number of elements). But if you use it on a pointer, you only get the size of the pointer. So, since in most cases where you'd like to know the size of an array, you only have a pointer, it's not all that useful.sepp2k– sepp2k2013年04月18日 18:26:52 +00:00Commented Apr 18, 2013 at 18:26 -
1@Jens The question is tagged [C++] and VLA did not make it into the C++ standard.authchir– authchir2013年04月18日 20:54:28 +00:00Commented Apr 18, 2013 at 20:54
because the entire sizeof "call" is calculated at compile time and whatever is between the parenthesis is discarded and not run at runtime,
the result is purely based on static type info available to the compiler
Why is sizeof called a compile-time operator?
Because, at compile time, the compiler calculates the sizeof the expression and substitutes that compile-time constant value.
Isn't it actually a run-time operator?
No.
You can even use sizeof
to evaluate the size of expressions you can't legally execute (ie, that would incur Undefined Behaviour), so long as the compiler can figure out what the type of the expression is.
Also, even before C++11 constexpr
, you can use sizeof
expressions in ways you can't use run-time expressions.
And if it is indeed a compile-time operator, how does it help in producing portable code ...
Types may vary in size on different platforms. Using sizeof
expressions instead of hard-coded assumptions means your code won't break when you compile on a different platform and your types change size.
-
1Well, I find this the most useful answer ;). Bro(assuming you to be male), I have a doubt. Do you mean to say that when people say that sizeof makes programs portable, they mean that the source code can be compiled without any errors in all machines and they don't mean that the the executable program can be run in any machine. Right ? If this is right, my doubt gets really cleared.Shravan– Shravan2013年04月19日 10:18:59 +00:00Commented Apr 19, 2013 at 10:18
-
1Yeah, the code is portable, in the sense you can compile a (different) correct binary for each platform.Useless– Useless2013年04月19日 10:34:41 +00:00Commented Apr 19, 2013 at 10:34
C++ doesn't actually store the metadata for objects at runtime so size checking must be compile time. For an example of how C++ doesn't validate size, declare an array of int
of some arbitrary size and read past the end of it. If you're lucky you'll get a segfault
but more likely you'll simply read gibberish, because C++ doesn't track the size of your array.
See Can a C/C++ program seg-fault from reading past the end of an array (UNIX)? for an example from SO.
sizeof(polymorphic_ptr*)
being constant is quite counter-intuitive and just silly. Yeah, it's the C++ way, but silly nevertheless.