[cfe-users] Clang Sizeof give diff value for Microsoft and Linux
David Blaikie via cfe-users
cfe-users at lists.llvm.org
Thu Feb 18 10:43:34 PST 2021
On Wed, Feb 17, 2021 at 9:17 PM Vivek Pandey <
vivek.pandey at tallysolutions.com> wrote:
> Dear David,
>
>
>
> Greeting!
>
>
>
> Please find details inline.
>
>
>
> Looking forward to hear from you.
>
>
>
>
>
> Best Regards,
>
>
>
> Vivek
>
>
>
> *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> 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.
>
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
>
>
>
>
>
>
>
> 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>
> *Sent:* Friday, January 29, 2021 3:23 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
>
>
>
> 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> 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,
>
> Vivek
>
>
>
> _______________________________________________
> cfe-users mailing list
> cfe-users at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
> <https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.llvm.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fcfe-users&data=04%7C01%7Cvivek.pandey%40tallysolutions.com%7C40d3553ff0ab4d51d60508d8d389c16b%7C66bcd9b727254893bb969ae424774af6%7C0%7C0%7C637491935908594708%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=EPGzLnXJVRqIymyU3TyndbVYJUkZ4QVk7mh%2FBJuop%2BI%3D&reserved=0>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20210218/3499271c/attachment.html>
More information about the cfe-users
mailing list