<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 17, 2021 at 9:17 PM Vivek Pandey <<a href="mailto:vivek.pandey@tallysolutions.com">vivek.pandey@tallysolutions.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div lang="EN-US" style="overflow-wrap: break-word;">
<div class="gmail-m_-5867239278139000407WordSection1">
<p class="MsoNormal">Dear David,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Greeting!<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Please find details inline.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Looking forward to hear from you.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Best Regards,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Vivek<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<p class="MsoNormal"><b>From:</b> David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> <br>
<b>Sent:</b> Thursday, February 18, 2021 2:50 AM<br>
<b>To:</b> Vivek Pandey <<a href="mailto:vivek.pandey@tallysolutions.com" target="_blank">vivek.pandey@tallysolutions.com</a>><br>
<b>Cc:</b> <a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a>; Manu Agarwal <<a href="mailto:manu.agarwal@tallysolutions.com" target="_blank">manu.agarwal@tallysolutions.com</a>>; Tuhin Sengupta <<a href="mailto:tuhin.sengupta@tallysolutions.com" target="_blank">tuhin.sengupta@tallysolutions.com</a>><br>
<b>Subject:</b> Re: [cfe-users] Clang Sizeof give diff value for Microsoft and Linux<u></u><u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Wed, Feb 17, 2021 at 12:08 AM Vivek Pandey <<a href="mailto:vivek.pandey@tallysolutions.com" target="_blank">vivek.pandey@tallysolutions.com</a>> wrote:<u></u><u></u></p>
</div>
<div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal">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
<span style="color:blue">sizeoff</span> to behave like MSVC or non-MSVC) <u></u><u></u></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><br>
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.<u></u><u></u></p>
<p class="MsoNormal"><span style="color:blue">[VP] If clang has that flag then it will definitely have libraries in both format. </span></p></div></div></div></div></div></blockquote><div><br>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).<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div lang="EN-US" style="overflow-wrap: break-word;"><div class="gmail-m_-5867239278139000407WordSection1"><div><div><div><p class="MsoNormal"><span style="color:blue">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.</span></p></div></div></div></div></div></blockquote><div><br>I don't understand what you mean by "on the whole image" verses "on individual objects".<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div lang="EN-US" style="overflow-wrap: break-word;"><div class="gmail-m_-5867239278139000407WordSection1"><div><div><div><p class="MsoNormal"><span style="color:blue"><u></u><u></u></span></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal">As in the current form it is breaking building of cross-platform application.<u></u><u></u></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><br>
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.<u></u><u></u></p>
<p class="MsoNormal"><span style="color:blue">[VP] Our is an application that exchanges binary data between same application that runs on diff platforms (</span>Windows/Linux/OSX/Android/iOS/….<span style="color:blue">). </span></p></div></div></div></div></div></blockquote><div><br>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.<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div lang="EN-US" style="overflow-wrap: break-word;"><div class="gmail-m_-5867239278139000407WordSection1"><div><div><div><p class="MsoNormal"><span style="color:blue">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 ?</span></p></div></div></div></div></div></blockquote><div><br>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.<br><br>- Dave<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div lang="EN-US" style="overflow-wrap: break-word;"><div class="gmail-m_-5867239278139000407WordSection1"><div><div><div><p class="MsoNormal"><u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><br>
<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">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. <u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<p class="MsoNormal"><b>From:</b> David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>>
<br>
<b>Sent:</b> Friday, January 29, 2021 3:23 AM<br>
<b>To:</b> Vivek Pandey <<a href="mailto:vivek.pandey@tallysolutions.com" target="_blank">vivek.pandey@tallysolutions.com</a>><br>
<b>Cc:</b> <a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a>; Manu Agarwal <<a href="mailto:manu.agarwal@tallysolutions.com" target="_blank">manu.agarwal@tallysolutions.com</a>>; Tuhin Sengupta <<a href="mailto:tuhin.sengupta@tallysolutions.com" target="_blank">tuhin.sengupta@tallysolutions.com</a>><br>
<b>Subject:</b> Re: [cfe-users] Clang Sizeof give diff value for Microsoft and Linux<u></u><u></u></p>
</div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<p class="MsoNormal">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).<u></u><u></u></p>
</div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<div>
<p class="MsoNormal">On Thu, Jan 28, 2021 at 1:45 PM Vivek Pandey via cfe-users <<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt">
<div>
<div>
<p class="MsoNormal">Hi Team,<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">We are using Clang 11 for our product that has common C++ code base for Windows, Linux, Macintosh, ….<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">We observed that
<span style="color:black;background:yellow">sizeof</span> operator gives different value on Windows and Linux/OSX, when the inheritance is from a common base class:<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Example Sample:<u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:8pt"> </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New"">#include <cassert></span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New"">struct Base {}; // empty class</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New"">struct Derived1 : Base {</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> int i;</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New"">};</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New"">struct Derived2 : Base {</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> Base c; // Base, occupies 1 byte, followed by padding for i</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> int i;</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New"">};</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New"">struct Derived3 : Base {</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> Derived1 c; // <span style="color:black;background:yellow">
Derived1 is too derived from same Base class</span></span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> int i;</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New"">};</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New"">int main()</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New"">{</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> <a href="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" target="_blank">
assert</a>(sizeof(Derived2) == 2*sizeof(int));</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New""> <a href="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" target="_blank">
assert</a>(<span style="color:black;background:yellow">sizeof(Derived3)</span> == 3*sizeof(int));</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:45.8pt">
<span style="font-size:8pt;font-family:"Courier New"">}</span><u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal"> When we compile above program using Clang 11 and run it on windows and Linux sizeof(<span style="font-size:10pt;font-family:"Courier New";color:black;background:yellow">Derived3</span>)
give different value.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">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.<u></u><u></u></p>
<p class="MsoNormal">Is there any flag that we can use so that it gives same result on all platform (Windows/Linux/OSX/Android/iOS/….)<u></u><u></u></p>
<p class="MsoNormal">Or another way to solve this gracefully.
<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Thank you!<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Best Regards,<u></u><u></u></p>
<p class="MsoNormal">Vivek<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<p class="MsoNormal">_______________________________________________<br>
cfe-users mailing list<br>
<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a><br>
<a href="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" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users</a><u></u><u></u></p>
</blockquote>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote></div></div>