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

Vivek Pandey via cfe-users cfe-users at lists.llvm.org
Wed Jan 27 23:09:27 PST 2021


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20210128/e6a38102/attachment.html>


More information about the cfe-users mailing list