I can't understand why this code prints 1 in C, but other digit in C++?
#include <stdio.h>
static char T = 'a';
int main(int argc, char** argv) {
struct T { char X[2]; };
printf("size of T is %zu\n", sizeof(T));
}
And why this code prints 1 in both C and C++?
#include <stdio.h>
int main(int argc, char** argv) {
static char T = 'a';
struct T { char X[2]; };
printf("size of T is %zu\n", sizeof(T));
}
Can somebody explain me this a little bit, please?
The size of the entire structure is 8 bytes. On knowing the structured padding, it is easier to redesign or rewrite the structure.
The sizeof operator is the most common operator in C. It is a compile-time unary operator and used to compute the size of its operand. It returns the size of a variable. It can be applied to any data type, float type, pointer type variables.
The sizeof for a struct is not always equal to the sum of sizeof of each individual member. This is because of the padding added by the compiler to avoid alignment issues. Padding is only added when a structure member is followed by a member with a larger size or at the end of the structure.
The sizeof operator gives the amount of storage, in bytes, required to store an object of the type of the operand. This operator allows you to avoid specifying machine-dependent data sizes in your programs.
Because in C
the struct is called struct T
and not only T
. In C++ the local definition of struct T
will hide the global variable T
:
#include <stdio.h>
static char T = 'a'; // (1)
int main(int argc, char** argv) {
// `struct T` shadows outer `T` in C++
struct T { char X[2]; }; // (2)
// C: sizeof char (1); C++: sizeof struct T (2)
printf("size of T is %u\n", sizeof(T));
// C/C++: sizeof struct T (2)
printf("size of struct T is %u\n", sizeof(struct T));
}
On the other hand, when both declarations are in the same naming context, the ambiguity of the identifier T
will result in the same results, since C++ expects you to specify that you really want to use the struct and not the char T
:
#include <stdio.h>
int main(int argc, char** argv) {
static char T = 'a';
struct T { char X[2]; };
printf("size of T is %u\n", sizeof(T)); // sizeof (char)
printf("size of struct T is %u\n", sizeof(struct T));// sizeof struct T
}
Which results in the same size for both C and C++.
Usually compilers do know that the identifier is ambiguous, but the warning is often hidden. Use compiler flags to show warnings, in GCC -Wall -Wextra
are the most useful for usual programming:
test.cc: In function »int main(int, char**)«: test.cc:5:43: Warning: unknown converting symbol »z« in format [-Wformat] test.cc:5:43: Warning: to many arguments for format [-Wformat-extra-args] test.cc: global: test.cc:3:5: Warning: unused parameter »argc« [-Wunused-parameter] test.cc:3:5: Warning: unused parameter »argv« [-Wunused-parameter] test.cc:2:13: Warning: »T« defined, but not used [-Wunused-variable]
In this case one will see that the global static char T
has been defined, but never used.
In C, when a structure is declared, it is of type struct <name of struct>
and not just the name of the struct. That is the reason. To avoid confusion, people use typedef
to simplify the declarations in C
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With