Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof struct or variable in C and C++

Tags:

c++

c

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?

like image 224
FrozenHeart Avatar asked Oct 11 '12 05:10

FrozenHeart


People also ask

What is the sizeof a struct in C?

The size of the entire structure is 8 bytes. On knowing the structured padding, it is easier to redesign or rewrite the structure.

Does sizeof work on variables?

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.

Does sizeof work on structs?

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.

What is sizeof () in C?

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.


2 Answers

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++.

How to avoid this mistakes

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.

like image 189
Zeta Avatar answered Oct 16 '22 15:10

Zeta


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

like image 27
psteelk Avatar answered Oct 16 '22 15:10

psteelk