Clang Sizeof give diff value for Microsoft and Linux

Hi Team,

We are using Clang 11 for our product that has common C++ code base for Windows, Linux, Macintosh, ….

We observed that sizeof operator gives different value on Windows and Linux/OSX, when the inheritance is from a common base class:

Example Sample:

#include

struct Base {}; // empty class

struct Derived1 : Base {

int i;

};

struct Derived2 : Base {

Base c; // Base, occupies 1 byte, followed by padding for i

int i;

};

struct Derived3 : Base {

Derived1 c; // Derived1 is too derived from same Base class

int i;

};

int main()

{

assert(sizeof(Derived2) == 2*sizeof(int));

assert(sizeof(Derived3) == 3*sizeof(int));

}

When we compile above program using Clang 11 and run it on windows and Linux sizeof(Derived3) give different value.

We don’t want a work-around via making first data member of derived class of type different from class that also is derived from same empty base class.

Is there any flag that we can use so that it gives same result on all platform (Windows/Linux/OSX/Android/iOS/….)

Or another way to solve this gracefully.

Thank you!

Best Regards,

Vivek

Clang on Windows is designed to be compatible with MSVC - which has different layout requirements than the Itanium ABI/GCC on Linux. I don’t think there’s a way to use the same ABI on both platforms - especially not if you are interacting with any code compiled by another compiler on both platforms (existing/foregin C++ precompiled libraries).

I think Clang, on/for windows, should give a compile time flag/option that can be used to control it (A flag when set make compile-time operator like sizeoff to behave like MSVC or non-MSVC) As in the current form it is breaking building of cross-platform application.

With C++11 onward many things are incorporated in standard so that one code base can be used across platform. Now if compiler is blocking the flow then it seems moving back to pre C++11.

I think Clang, on/for windows, should give a compile time flag/option that can be used to control it (A flag when set make compile-time operator like sizeoff to behave like MSVC or non-MSVC)

But that would break the ability for that code to call existing libraries (including the MS runtime), I think - which would be quite broken/unusable, so far as I know.

As in the current form it is breaking building of cross-platform application.

What dependence does this application have on the size of certain structures? That seems quite not-cross-platform to me & the code probably should be changed to be flexible to the different size of layouts on different platforms.

Dear David,

Greeting!

Please find details inline.

Looking forward to hear from you.

Best Regards,

Vivek

Dear David,

Greeting!

Please find details inline.

Looking forward to hear from you.

Best Regards,

Vivek

From: David Blaikie <dblaikie@gmail.com>
Sent: Thursday, February 18, 2021 2:50 AM
To: Vivek Pandey <vivek.pandey@tallysolutions.com>
Cc: cfe-users@lists.llvm.org; Manu Agarwal <manu.agarwal@tallysolutions.com>; Tuhin Sengupta <tuhin.sengupta@tallysolutions.com>
Subject: Re: [cfe-users] Clang Sizeof give diff value for Microsoft and Linux

I think Clang, on/for windows, should give a compile time flag/option that can be used to control it (A flag when set make compile-time operator like sizeoff to behave like MSVC or non-MSVC)

But that would break the ability for that code to call existing libraries (including the MS runtime), I think - which would be quite broken/unusable, so far as I know.

[VP] If clang has that flag then it will definitely have libraries in both format.

What will have libraries in both formats? third parties will publish libraries in both formats? But there wouldn’t be any way for the compiler to know which format the library was compiled in, or any error message for the linker to provide - you’d get silently broken code. And it’s the system libraries I’m most concerned about (since it’s not likely they would be published in both formats).

Plus the system libraries works on the whole image and not on individual objects within the image and thus don’t seems to break any compatibility.

I don’t understand what you mean by “on the whole image” verses “on individual objects”.

As in the current form it is breaking building of cross-platform application.

What dependence does this application have on the size of certain structures? That seems quite not-cross-platform to me & the code probably should be changed to be flexible to the different size of layouts on different platforms.

[VP] Our is an application that exchanges binary data between same application that runs on diff platforms (Windows/Linux/OSX/Android/iOS/….).

That’s not portable for a bunch of reasons - the layout of structs (as you’ve found), the size of types (“int” isn’t the same size on all platforms, for instance), etc.

As I mentioned in my initial email it’s a structure/class declaration and we are using clang to build on all these different platform. Isn’t the behavior of ‘sizeof’ operator be consistent ?

Not at all - sizeof exists because the size of a struct is different on different C++ platforms/implementations - to write portable code in C++ you must write it in such a way that you don’t depend on the answer being any particular value, nor the same value across platforms. sizeof exists so you can query the implementation you’re currently running on and adjust your program’s behavior based on that result as needed.

  • Dave