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

David Blaikie via cfe-users cfe-users at lists.llvm.org
Thu Jan 28 13:52:56 PST 2021


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 <http://en.cppreference.com/w/cpp/error/assert>(sizeof(Derived2)
> == 2*sizeof(int));
>
>
>
>     assert <http://en.cppreference.com/w/cpp/error/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
>
>
> _______________________________________________
> cfe-users mailing list
> cfe-users at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20210128/735f65aa/attachment-0001.html>


More information about the cfe-users mailing list