简体   繁体   中英

how compiler works to evaluate sizeof operator in c

though there are already several questions asked on this forum & others related to sizeof operator, i could not get any answer on how compiler evaluates the sizeof operator to find the size of any datatype, variable, pointer,array etc. if possible also point me to some links which can help me understand this in detail. any help will be greatly appreciated. thanks.

The compiler just knows the size of primitive datatypes; this knowledge is fundamentally built in to the compiler.

For traditional fixed-size arrays and complex data types (structs and classes), it just adds up the sizes of the constituent primitives and accounts for any necessary padding. See http://en.wikipedia.org/wiki/Data_structure_alignment

The sizeof() calculation is done at compile-time in most cases. The exception is for variable-length arrays (new in C99), where it is calculated at runtime once the number of elements is known.

The size information comes from information the compiler has internally.

The compiler knows the size of basic types such as char , int , double , and pointers because these are designed into the compiler.

The compiler calculates the size of compound structures by adding the sizes of all their parts, including members in the structure, plus any padding used for alignment, plus any hidden parts that support language features (such as a pointer to class information to support polymorphism).

The compiler calculates the size of an array by multiplying the number of elements by the size of each element.

char has a size of 1, by definition. Note that it's not necessarily 8 bits, but it is always 1 byte.

The primitive types, and also pointers, each have a fixed size which the compiler knows. These may vary between compilers and platforms.

Arrays are calculated from the size of the element type multiplied by the number of elements.

Structures in C, and classes in C++, are at least as large as the sum of their non-static data members. They may be larger in order to correctly align the members; and in C++ there may be extra, inaccessible, data members to support polymorphism.

The size of variables declared in source code are always know to a program whether they are arrays, structs, or etc. But most data types have fixed sizes which define them. The reason we have different data types is because we need different sized data types. A char is any character that can fit in 1 byte. Which for the sake of this example can only represent the numbers 0-9. So lets say we have a 16 digit long number, if we only have chars then it would take 16 bytes for a computer to represent that number. Know imagine an 8 digit long number(which is a byte) that can only have 2 possible values(0 or 1) in each of its 8 digits, you can only have 64 possible unique combinations. Now imagine if everyone of those 64 combinations represented a specific number. You could most likely right that 16 digit long number in less than 16 bytes. That is the idea behind the int data type. Please note that I simplified the explanation of int and chars so that I could explain them without having to get too in depth. Now I know what your thinking what about arrays?

for (i = 0; array[i] != NULL; i++) {}

The above code is basically what the function strlen() does. Have you ever wondered why arrays are terminated with NULL characters? It is partly because it makes finding the length of an array as easy as the above code. So to answer your question sizeof() really dosent calculate anything it already knows the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM