developer.ahm
developer.ahm

Reputation: 180

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

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

Upvotes: 4

Views: 557

Answers (4)

iBug
iBug

Reputation: 37217

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.

Upvotes: 16

Lundin
Lundin

Reputation: 213276

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.

Upvotes: 3

haccks
haccks

Reputation: 105992

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.

Upvotes: 4

FrankM
FrankM

Reputation: 822

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.

Upvotes: 2

Related Questions