For example result of this code snippet depends on which machine: the compiler machine or the machine executable file works?
sizeof(short int)
For example result of this code snippet depends on which machine: the compiler machine or the machine executable file works?
sizeof(short int)
sizeof
is a compile time operator.
It depends on the machine executing your program. But the value evaluates at compile time. Thus the compiler (of course) has to know for which machine it's compiling.
sizeof (void*)
will be 8 if you compile for a 64 bit platform (assuming 8bit chars), regardless of the machine you build the program on. The sizeof of short int
is unlikely to change. –
Roumell As of C99, sizeof is evaluated at runtime if and only if the operand is a variable-length array, e.g. int a[b], where b is not known at compile time. In this case, sizeof(a) is evaluated at runtime and its result is the size (in bytes) of the entire array, i.e. the size of all elements in the array, combined. To get the number of elements in the array, use sizeof(a) / sizeof(b)
. From the C99 standard:
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
Note that all of this is different from what you'd get if you allocated an array on the heap, e.g. int* a = new int[b]
. In that case, sizeof(a) would just give you the size of a pointer to int, i.e. 4 or 8 bytes, regardless of how many elements are in the array.
sizeof is evaluated at compile time, but if the executable is moved to a machine where the compile time and runtime values would be different, the executable will not be valid.
sizeof
would be calculated at compile time and then hard-coded in the executable"? I used to think so, but now I am not able to explain how this works. b) Also, by "not valid" so you mean that it will not run at all or only that the executable will report wrong results? –
Socinus #define BIT_SIZE(T) (sizeof(T) * CHAR_BIT)
can be used as a template parameter for a bitset, such as std::bitset<BIT_SIZE(int)>
, while it's determined at run time for C99's variable-length arrays. –
Perk std::bitset<BIT_SIZE(someVar)>(someVar)
, if creating the bitset from a pre-existing variable. –
Perk Anon tried to explain this, but still he nor no one else has stated that your compiler has flags to indicate what processor you are compiling for. This is how sizeof short is known at compile time.
I however feel that any desktop compiler should push out code compatible with desktops. I think the OS provides certain abstractions around this. Even though I hear that windows machines have different architecture from Macintosh machines.
© 2022 - 2024 — McMap. All rights reserved.
sizeof
operator is evaluated at runtime, specifically when applied to VLAs (variable length arrays). – Wren