<html><head></head><body lang="en-US" style="background-color: rgb(255, 255, 255); line-height: initial;"> <div style="width: 100%; font-size: initial; font-family: Calibri, 'Slate Pro', sans-serif, sans-serif; color: rgb(31, 73, 125); text-align: initial; background-color: rgb(255, 255, 255);">That's not UB, just indeterminate. </div> <div style="width: 100%; font-size: initial; font-family: Calibri, 'Slate Pro', sans-serif, sans-serif; color: rgb(31, 73, 125); text-align: initial; background-color: rgb(255, 255, 255);"><br style="display:initial"></div> <div style="font-size: initial; font-family: Calibri, 'Slate Pro', sans-serif, sans-serif; color: rgb(31, 73, 125); text-align: initial; background-color: rgb(255, 255, 255);">Sent from my BlackBerry portable Babbage Device</div> <table width="100%" style="background-color:white;border-spacing:0px;"> <tbody><tr><td colspan="2" style="font-size: initial; text-align: initial; background-color: rgb(255, 255, 255);"> <div style="border-style: solid none none; border-top-color: rgb(181, 196, 223); border-top-width: 1pt; padding: 3pt 0in 0in; font-family: Tahoma, 'BB Alpha Sans', 'Slate Pro'; font-size: 10pt;"> <div><b>From: </b>Richard Hodges</div><div><b>Sent: </b>Saturday, November 25, 2017 12:59 PM</div><div><b>To: </b>std-***@isocpp.org</div><div><b>Reply To: </b>std-***@isocpp.org</div><div><b>Subject: </b>Re: [std-discussion] Value of padding bytes</div></div></td></tr></tbody></table><div style="border-style: solid none none; border-top-color: rgb(186, 188, 209); border-top-width: 1pt; font-size: initial; text-align: initial; background-color: rgb(255, 255, 255);"></div><br><div id="_originalContent" style=""><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On 25 November 2017 at 18:37, Tony V E <span dir="ltr"><<a href="mailto:***@gmail.com" target="_blank">***@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="en-US" style="background-color:rgb(255,255,255);line-height:initial"> <div style="width:100%;font-size:initial;font-family:Calibri,'Slate Pro',sans-serif,sans-serif;color:rgb(31,73,125);text-align:initial;background-color:rgb(255,255,255)">You are surprised the padding bytes are a different value than the other way?</div></div></blockquote><div><br></div><div>No, but it's a nice unequivocal demonstration of the UB-ness when compared with the defined way of doing it (previous post). </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="en-US" style="background-color:rgb(255,255,255);line-height:initial"><div style="width:100%;font-size:initial;font-family:Calibri,'Slate Pro',sans-serif,sans-serif;color:rgb(31,73,125);text-align:initial;background-color:rgb(255,255,255)"><br></div> <div style="width:100%;font-size:initial;font-family:Calibri,'Slate Pro',sans-serif,sans-serif;color:rgb(31,73,125);text-align:initial;background-color:rgb(255,255,255)"><br style="display:initial"></div> <div style="font-size:initial;font-family:Calibri,'Slate Pro',sans-serif,sans-serif;color:rgb(31,73,125);text-align:initial;background-color:rgb(255,255,255)">Sent from my BlackBerry <wbr>portable Babbage Device</div> <table width="100%" style="background-color:white;border-spacing:0px"> <tbody><tr><td colspan="2" style="font-size:initial;text-align:initial;background-color:rgb(255,255,255)"> <div style="border-style:solid none none;border-top-color:rgb(181,196,223);border-top-width:1pt;padding:3pt 0in 0in;font-family:Tahoma,'BB Alpha Sans','Slate Pro';font-size:10pt"> <div><b>From: </b>Richard Hodges</div><div><b>Sent: </b>Saturday, November 25, 2017 12:29 PM</div><div><b>To: </b><a href="mailto:std-***@isocpp.org" target="_blank">std-***@isocpp.org</a></div><div><b>Reply To: </b><a href="mailto:std-***@isocpp.org" target="_blank">std-***@isocpp.org</a></div><div><b>Subject: </b>Re: [std-discussion] Value of padding bytes</div></div></td></tr></tbody></table><div><div class="h5"><div style="border-style:solid none none;border-top-color:rgb(186,188,209);border-top-width:1pt;font-size:initial;text-align:initial;background-color:rgb(255,255,255)"></div><br><div id="m_-4408439978304409036_originalContent"><div dir="ltr">Sorry for the spam-post.<div><br></div><div>Interestingly, refactoring the program to use the UB method does indeed produce surprising output (gcc, -O3):</div><div><br></div><div><div><font face="monospace, monospace">#include <cstring></font></div><div><font face="monospace, monospace">#include <type_traits></font></div><div><font face="monospace, monospace">#include <memory></font></div><div><font face="monospace, monospace">#include <iostream></font></div><div><font face="monospace, monospace">#include <iomanip></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">struct X { char c; double d; };</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">int main() {</font></div><div><font face="monospace, monospace"> X x = { 'a', 1.1 };</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> unsigned char* byte_buffer = reinterpret_cast<unsigned char *>(std::addressof(x));</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> auto first = byte_buffer, last = byte_buffer + sizeof(x);</font></div><div><font face="monospace, monospace"> for( ; first != last ; ++first )</font></div><div><font face="monospace, monospace"> {</font></div><div><font face="monospace, monospace"> std::cout << std::hex << std::setw(2) << std::setfill('0') << unsigned(*first) << ", ";</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace">}</font></div></div><div><br></div><div>example output:</div><div><br></div><div><font face="monospace, monospace">61, 09, 40, 00, 00, 00, 00, 00, 9a, 99, 99, 99, 99, 99, f1, 3f, <br></font></div><div><font face="monospace, monospace"> ^^ ^^ !!!</font></div><div><font face="monospace, monospace"><br></font></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 25 November 2017 at 18:19, Richard Hodges <span dir="ltr"><<a href="mailto:***@gmail.com" target="_blank">***@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Restating the question in terms of memcpy to an array:<div><br></div><div>I don't think the program below is in any way undefined behaviour. </div><div>It is perfectly (implementation) defined how many hex values will appear on stdout. </div><div>The values of the bytes themselves is implementation defined or undefined, depending on the byte, but they are values nonetheless. They must be. They were created on line 13 and initialised on line 14. It's unequivocal.</div><div><br></div><div>The fact that we can memcpy an object to data and then back in a defined way must implicitly imply that all bytes in the object X are validly readable and writeable. </div><div>It seems to me that they cannot be trap values otherwise memcpy could not work.</div><div><br></div><div><div><font face="monospace, monospace">#include <cstring></font></div><div><font face="monospace, monospace">#include <type_traits></font></div><div><font face="monospace, monospace">#include <memory></font></div><div><font face="monospace, monospace">#include <iostream></font></div><div><font face="monospace, monospace">#include <iomanip></font></div><span><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">struct X { char c; double d; };</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">int main() {</font></div><div><font face="monospace, monospace"> X x = { 'a', 1.1 };</font></div><div><font face="monospace, monospace"><br></font></div></span><div><font face="monospace, monospace"> unsigned char byte_buffer[sizeof(X)];</font></div><div><font face="monospace, monospace"> std::memcpy(byte_buffer, std::addressof(x), sizeof(x));</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> for(auto uc : byte_buffer)</font></div><div><font face="monospace, monospace"> {</font></div><div><font face="monospace, monospace"> std::cout << std::hex << std::setw(2) << std::setfill('0') << unsigned(uc) << ", ";</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace">}</font></div></div><div><br></div><div>example output:</div><div><br></div><div><font face="monospace, monospace">61, 00, 00, 00, 00, 00, 00, 00, 9a, 99, 99, 99, 99, 99, f1, 3f, <br></font></div><div><br></div><div>link: <a href="http://coliru.stacked-crooked.com/a/408e699c9fc98afd" target="_blank">http://coliru.stacked-cr<wbr>ooked.com/a/408e699c9fc98afd</a></div><div><br></div><div>The original version, using a reinterpret_cast seems to me to be strictly undefined behaviour because X "is not a" char [] (yes I know it is really, but in compiler memory model doublespeak it's conceptually not).</div><div>Since the compiler will see you doing something UBish it's within its rights to produce surprising code.</div><div><br></div><div>Using reinterpret_cast to cast a pointer-to-Thing to pointer-to-SomethingElse is UB if you access the SomethingElse, no?</div><div><br></div><div>As a parting opinion, I think it's perfectly ok to define the c++ language this way, but I really do think the standard should compel compilers to emit a diagnostic in the case of unequivocal UB like this (ideally with a suggestion of the correct approach). </div><div><br></div><div>Otherwise, this bytes-or-objects argument is set to run and run.</div><div><br></div><div> </div></div><div class="m_-4408439978304409036HOEnZb"><div class="m_-4408439978304409036h5"><div class="gmail_extra"><br><div class="gmail_quote">On 25 November 2017 at 01:06, Thiago Macieira <span dir="ltr"><<a href="mailto:***@macieira.org" target="_blank">***@macieira.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On quarta-feira, 22 de novembro de 2017 08:56:29 PST Hyman Rosen wrote:<br> </span><span>> Does the following code have undefined behavior (when there are padding<br>
> bytes)?<br>
><br>
> I would say no, for a variety of reasons - objects may be accessed through<br>
> char glvalues,<br>
> the bytes of an object can be copied out and copied back in, the C++ memory<br>
> model is<br>
> intended to be compatible with C, etc., but I don't see an explicit<br>
> statement in the Standard.<br>
><br>
> Tools like valgrind complain about accessing uninitialized data in cases<br>
> like this too, which<br>
> is annoying.<br>
><br>
> struct X { char c; double d; };<br>
> int main() {<br>
> X x = { 'a', 1.1 };<br>
> return reinterpret_cast<char*>(&x)[1] == 0;<br>
> }<br>
<br>
</span>I think we should ask the question:<br>
<br>
Why do you care? What are you trying to do that reduced to the case above?<br>
<span class="m_-4408439978304409036m_-1424870224114001820im m_-4408439978304409036m_-1424870224114001820HOEnZb"><br>
--<br>
Thiago Macieira - thiago (AT) <a href="http://macieira.info" rel="noreferrer" target="_blank">macieira.info</a> - thiago (AT) <a href="http://kde.org" rel="noreferrer" target="_blank">kde.org</a><br>
Software Architect - Intel Open Source Technology Center<br>
<br>
</span><div class="m_-4408439978304409036m_-1424870224114001820HOEnZb"><div class="m_-4408439978304409036m_-1424870224114001820h5">--<br>
<br>
---<br>
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-discussion%***@isocpp.org" target="_blank">std-discussion+***@iso<wbr>cpp.org</a>.<br>
To post to this group, send email to <a href="mailto:std-***@isocpp.org" target="_blank">std-***@isocpp.org</a>.<br>
Visit this group at <a href="https://groups.google.com/a/isocpp.org/group/std-discussion/" rel="noreferrer" target="_blank">https://groups.google.com/a/is<wbr>ocpp.org/group/std-discussion/</a><wbr>.<br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
<p></p>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-discussion+***@isocpp.org" target="_blank">std-discussion+unsubscribe@<wbr>isocpp.org</a>.<br>
To post to this group, send email to <a href="mailto:std-***@isocpp.org" target="_blank">std-***@isocpp.org</a>.<br>
Visit this group at <a href="https://groups.google.com/a/isocpp.org/group/std-discussion/" target="_blank">https://groups.google.com/a/<wbr>isocpp.org/group/std-<wbr>discussion/</a>.<br>
<br></div></div></div></div><div class="HOEnZb"><div class="h5">
<p></p>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-discussion+***@isocpp.org" target="_blank">std-discussion+unsubscribe@<wbr>isocpp.org</a>.<br>
To post to this group, send email to <a href="mailto:std-***@isocpp.org" target="_blank">std-***@isocpp.org</a>.<br>
Visit this group at <a href="https://groups.google.com/a/isocpp.org/group/std-discussion/" target="_blank">https://groups.google.com/a/<wbr>isocpp.org/group/std-<wbr>discussion/</a>.<br>
</div></div></blockquote></div><br></div></div>
<p></p>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-discussion+***@isocpp.org">std-discussion+***@isocpp.org</a>.<br>
To post to this group, send email to <a href="mailto:std-***@isocpp.org">std-***@isocpp.org</a>.<br>
Visit this group at <a href="https://groups.google.com/a/isocpp.org/group/std-discussion/">https://groups.google.com/a/isocpp.org/group/std-discussion/</a>.<br>
<br><!--end of _originalContent --></div></body></html>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-discussion+***@isocpp.org">std-discussion+***@isocpp.org</a>.<br />
To post to this group, send email to <a href="mailto:std-***@isocpp.org">std-***@isocpp.org</a>.<br />
Visit this group at <a href="https://groups.google.com/a/isocpp.org/group/std-discussion/">https://groups.google.com/a/isocpp.org/group/std-discussion/</a>.<br />