[cfe-users] Clang Sizeof give diff value for Microsoft and Linux

Vivek Pandey via cfe-users cfe-users at lists.llvm.org
Wed Feb 17 21:17:14 PST 2021

Dear David,


Please find details inline.

Looking forward to hear from you.

Best Regards,


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

On Wed, Feb 17, 2021 at 12:08 AM Vivek Pandey <vivek.pandey at tallysolutions.com<mailto:vivek.pandey at tallysolutions.com>> wrote:
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. 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.
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/….). 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 ?

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.

From: David Blaikie <dblaikie at gmail.com<mailto:dblaikie at gmail.com>>
Sent: Friday, January 29, 2021 3:23 AM
To: Vivek Pandey <vivek.pandey at tallysolutions.com<mailto:vivek.pandey at tallysolutions.com>>
Cc: cfe-users at lists.llvm.org<mailto:cfe-users at lists.llvm.org>; Manu Agarwal <manu.agarwal at tallysolutions.com<mailto:manu.agarwal at tallysolutions.com>>; Tuhin Sengupta <tuhin.sengupta at tallysolutions.com<mailto:tuhin.sengupta at tallysolutions.com>>
Subject: Re: [cfe-users] Clang Sizeof give diff value for Microsoft and Linux

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).

On Thu, Jan 28, 2021 at 1:45 PM Vivek Pandey via cfe-users <cfe-users at lists.llvm.org<mailto:cfe-users at lists.llvm.org>> wrote:
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 <cassert>

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<https://ind01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ferror%2Fassert&data=04%7C01%7Cvivek.pandey%40tallysolutions.com%7C40d3553ff0ab4d51d60508d8d389c16b%7C66bcd9b727254893bb969ae424774af6%7C0%7C0%7C637491935908584715%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=4g2xoIiTQAfQdgb989k2AsCPo8Q0qNa5ur6n5vfhJLg%3D&reserved=0>(sizeof(Derived2) == 2*sizeof(int));

    assert<https://ind01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ferror%2Fassert&data=04%7C01%7Cvivek.pandey%40tallysolutions.com%7C40d3553ff0ab4d51d60508d8d389c16b%7C66bcd9b727254893bb969ae424774af6%7C0%7C0%7C637491935908584715%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=4g2xoIiTQAfQdgb989k2AsCPo8Q0qNa5ur6n5vfhJLg%3D&reserved=0>(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,

cfe-users mailing list
cfe-users at lists.llvm.org<mailto:cfe-users at lists.llvm.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20210218/5deeb8d2/attachment-0001.html>

More information about the cfe-users mailing list