Why does the size of the same identifier differ in C and C++?

微笑、不失礼 提交于 2019-12-12 12:23:12

问题


#include <stdio.h>
int T;
int main()
{
    struct T { double x; };  
    printf("%zu", sizeof(T));
    return 0;
}

If I run this code in C, the result is 4, while in C++ it is 8.

Can someone explain why the difference?


回答1:


Short answer: Because they aren't the same identifier, in fact.

In C, structure names and variable names fall into different namespaces, so in C,

sizeof(T) == sizeof(int) // global variable T
sizeof(struct T) == sizeof(struct T) // not the same namespace

In C++, however, structure/class names goes into the same namespace as variables. The "nearest" (most local) name is the result of name lookup, so now

sizeof(T) == sizeof(struct T) // structure T
sizeof(::T) == sizeof(int) // Note the scope resolution operator

And therefore the result is 4 and 8, respectively.

In C++, you can get 4 with sizeof(::T). The "double-colon" scope resolution operator forces the compiler to take T as the name in external namespace, so ::T is the variable of type int that you want.


In C, (structures/unions/enums) and (variables/functions/typedefs) have separate namespaces, so you can write this without worrying about names conflicting.

struct T T;

note the parentheses, that structs, unions and enums share one namespace while the other three share another.

If you try to do this in C++, you'll immediately run into problems.


I like hacck's comment. He got the point that the fundamental reason is that C and C++ are different languages, despite their similarity.




回答2:


You are getting different results because C and C++ are different languages.

In C++, when you declare a struct (a special class),

struct T { 
    double x; 
};  

then you can use it as

T sobj;  // sobj is an object of type T

T is a type here. while in C, T is not a type but struct T is

struct T sobj; 

Now use sizeof(T) or sizeof(struct T) in C++ and sizeof(struct T) in C and they will give you the size of struct.




回答3:


In C, the T in struct T is a struct tag. Struct tags reside in a different namespace than variables and types. To get a type in C, you would have to write struct T.

sizeof() expects either a type or an expression, and a struct tag is neither. You do you however have the variable T. A variable name is a valid expression in sizeof().

In C++ however, the T in struct T is a type name since a struct in C++ is essentially just a class with all members public. Therefore sizeof(T) matches the struct type in C++.


Notable here is that the sizeof operator has two different syntax cases:

sizeof unary-expression
sizeof ( type-name )

In case of the former, sizeof only accepts expressions, but not types. In case of the latter, it excepts either expressions or types. So had you written sizeof T instead of sizeof (T), you would have gotten a compiler error in C++, since T would then have been a type and not an expression.




回答4:


I believe in C to refer to the structure you would need to write struct T while in C++ since a struct is a public class it can be refered to as just T So both are just taking the most local definition of T they have.



来源:https://stackoverflow.com/questions/47347461/why-does-the-size-of-the-same-identifier-differ-in-c-and-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!