- C language
- Basic grammar
- here
sizeof operator
The sizeof operator returns the size of a type if that type is an element of an array. The unit is bytes.
sizeof (type name)
The confusing expression "the size of a type when that type is an element of an array" is because the sizeof operator does not always return the defined size. You can see the difference when you look at the size of the structure with the sizeof operator.
Numeric type size
First, int32_t, int64_t, float, double type.
#include <stdint.h> #include <stdio.h> int main (void) { printf("int32_t:%d, int64_t:%d, float:%d, double:%d", sizeof (int32_t), sizeof (int64_t), sizeof (float), sizeof (double)); }
This is the output result. int32_t is 4 bytes, int64_t is 8 bytes, float is 4 bytes, and double is 8 bytes.
int32_t: 4, int64_t: 8, float: 4, double: 8
Void * type type size
Let's take a look at the size of the general-purpose pointer type "void *". The sizes of generic pointer types and other pointer types always match.
A CPU with a memory space of 32 bits has 4 bytes, and a CPU with a memory space of 64 bits has 8 bytes.
#include <stdio.h> int main (void) { printf("void *:%d\n", sizeof (void *)); }
Union size
If you define a union, it will be the size if the type of the size of the largest member is an element of the array.
#include <stdint.h> #include <stdio.h> union myapp_value { int8_t int8_val; int16_t int16_val; int32_t int32_val; int64_t int64_val; };; int main (void) { printf("union myapp_value:%d\n", sizeof (union myapp_value)); }
Output result.
union myapp_value: 8
It is now the same size as the largest member, int64_t.
Structure size
Understanding the size of the structure is a bit tricky. So what value does the following sizeof operator return?
#include <stdint.h> #include <stdio.h> struct myapp_data { int64_t int64_val; int8_t int8_val1; int8_t int8_val2; };; int main (void) { printf("struct myapp_data:%d\n", sizeof (struct myapp_data)); }
int64_t is 8 bytes, int8_t is 2 bytes, and the total is 10 bytes.
The output result is as follows.
struct myapp_data: 16
Oh, the result is 16. why?
This is because, as I wrote at the beginning, the sizeof operator returns "the size of the type when the type is an element of the array" rather than the size of the type.
Structure alignment
How is "the size of the type when that type is an element of the array" determined?
The following rules.
The size of the structure returned by sizeof is always a multiple of the maximum size of the member variable (in the above example, the largest int64_t is 8 bytes, so it is a multiple of 8). Members of smaller sizes are sorted from the beginning to a position that does not exceed a multiple of the maximum size of the member variable. However, the member variables are sorted from the position that is a multiple of the size of the member variable.
These are called data alignments. Think of this as a rule for arranging data in a position that speeds up CPU operations.
The gaps that are lined up according to the above rules are called padding.
// sizeof returns 16 struct myapp_data { int64_t int64_val; // 8 bytes int8_t int8_val1; // 1 byte int8_t int8_val2; // 1 byte // 6 byte padding };;
Now let's take a look at some examples.
// sizeof returns 16 struct myapp_data { int64_t a; // 8 bytes int8_t b; // 1 byte int8_t c; // 1 byte int32_t d; // 4 bytes // 2-byte padding };;
// sizeof returns 24 struct myapp_data { int64_t a; // 8 bytes int8_t b; // 1 byte int8_t c; // 1 byte int32_t d; // 4 bytes // 2-byte padding int32_t e; // 4 bytes // 4-byte padding };;
// sizeof returns 12 struct myapp_data { int32_t a; // 4 bytes int8_t b; // 1 byte int8_t c; // 1 byte // 2-byte padding int32_t d; // 4 bytes };;
// sizeof returns 16 struct myapp_data { int32_t a; // 4 bytes int8_t b; // 1 byte // 1 byte padding int16_t c; // 2 bytes int8_t d; // 1 byte // 3 byte padding int32_t e; // 4 bytes };;