问题
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.
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
I guess the sizeof
on vector or other STL container depends on specific environment.
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?
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?
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:
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.
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.
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
.
回答2:
sizeof
is not a macro. It is a built-in operator and yields a constant expression, so you can imagine that the compiler just replaces it at compile-time with a literal whose value is the size of the object or class operand.Functions don't take up space inside objects. The code for the function is not stored inside the object.
The size of a container such as
std::vector<int>
is the amount of memory taken up by the vector's members, plus some implementation-defined padding. It does not include any memory that the vector "owns" by holding pointers. (Note that elements of the vector are not members---when I say members I mean the members declared in the definition of thestd::vector
class.) So it's independent of how many elements are inside the vector.
来源:https://stackoverflow.com/questions/24942048/what-is-sizeof-operator-doing-in-c