Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

name-lookup

#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?

like image 426
developer.ahm Avatar asked Nov 29 '22 00:11

developer.ahm


1 Answers

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.

like image 168
iBug Avatar answered Dec 09 '22 16:12

iBug