简体   繁体   English

C和C ++中的sizeof结构或变量

[英]sizeof struct or variable in C and C++

I can't understand why this code prints 1 in C, but other digit in C++? 我不明白为什么这段代码在C语言中显示1,而在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++? 为什么这段代码在C和C ++中都显示1?

#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? 有人可以向我解释一下吗?

Because in C the struct is called struct T and not only T . 由于C的结构被称为struct T不仅T In C++ the local definition of struct T will hide the global variable T : 在C ++中, struct T的局部定义将隐藏全局变量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 : 另一方面,当两个声明都在相同的命名上下文中时,标识符T的歧义将导致相同的结果,因为C ++希望您指定您确实要使用struct而不是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++. C和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: 在GCC中,使用编译器标志显示警告, -Wall -Wextra对于常规编程最有用:

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. 在这种情况下,将看到已定义全局static char T ,但从未使用过。

In C, when a structure is declared, it is of type struct <name of struct> and not just the name of the struct. 在C中,当声明一个结构时,它的类型为struct <name of struct> ,而不仅仅是该结构的名称。 That is the reason. 这就是原因。 To avoid confusion, people use typedef to simplify the declarations in C 为避免混淆,人们使用typedef简化了C中的声明

As mentioned by others, in the first snippet the local struct T will hide the static var in C++ . 正如其他人提到的那样,在第一个代码片段中,本地struct T将在C++隐藏静态var。 However, to fully understand what's going on, you also have to talk about name disambiguation. 但是,要完全了解正在发生的事情,您还必须谈论名称歧义消除。 In the second code snippet, struct T isn't hiding T - they're in the same scope. 在第二个代码段中, struct T没有隐藏T它们在同一范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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