<div dir="ltr"><div><div>I now know this is referred to as the "struct hack."</div><div><br></div><div>This really is undefined behavior in the C++11 standard...  there was a proposal to make the struct hack valid for C++11:</div><div><br></div><div><a href="https://www.google.com/url?sa=D&q=http%3A%2F%2Fwww.open-std.org%2Fjtc1%2Fsc22%2Fwg14%2Fwww%2Fdocs%2Fn791.htm" target="_blank" style="text-decoration-line:none;border:0px;font-size:13px;font-family:arial;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(102,17,204)">http://www.open-std.org/jtc1/sc22/wg14/www/docs/n791.htm</a><br></div><div><br></div><div>From another google engineer:</div><div><br></div><div>5.2.1 tells you that x[y] is identical to *(x + y). </div><div>5.7.5 tells you that x + y has undefined behavior if the result points outside of the array.</div></div><div><br></div><div>From the C++11 standard:</div><div><br></div><div>5.2.1 Subscripting [expr.sub] </div><div>1 A postfix expression followed by an expression in square brackets is a postfix expression. One of the expressions </div><div>shall have the type “pointer to T” and the other shall have unscoped enumeration or integral type. </div><div>The result is an lvalue of type “T.” The type “T” shall be a completely-defined object type.62 The expression </div><div>E1[E2] is identical (by definition) to *((E1)+(E2)) [ Note: see 5.3 and 5.7 for details of * and + and 8.3.4 </div><div>for details of arrays. — end note ]</div><div><br></div><div>5.7 Additive operators </div><div>...</div><div><br></div><div>5.7.5 When an expression that has integral type is added to or subtracted from a pointer, the result has the type </div><div>of the pointer operand. If the pointer operand points to an element of an array object, and the array is </div><div>large enough, the result points to an element offset from the original element such that the difference of </div><div>the subscripts of the resulting and original array elements equals the integral expression. In other words, if </div><div>the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) </div><div>and (P)-N (where N has the value n) point to, respectively, the i + n-th and i − n-th elements of the array </div><div>object, provided they exist. Moreover, if the expression P points to the last element of an array object, </div><div>the expression (P)+1 points one past the last element of the array object, and if the expression Q points </div><div>one past the last element of an array object, the expression (Q)-1 points to the last element of the array </div><div>object. If both the pointer operand and the result point to elements of the same array object, or one past </div><div>the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is </div><div>undefined.</div><div><br></div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, May 16, 2017 at 7:35 AM, Even Rouault <span dir="ltr"><<a href="mailto:even.rouault@spatialys.com" target="_blank">even.rouault@spatialys.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>
<div style="font-family:"sans serif";font-size:9pt;font-weight:400;font-style:normal"><span class="gmail-">
<p style="margin:0px;text-indent:0px">On mardi 16 mai 2017 07:01:53 CEST Kurt Schwehr wrote:</p>
<p style="margin:0px;text-indent:0px">> w.r.t. <a href="https://trac.osgeo.org/gdal/changeset/38405" target="_blank">https://trac.osgeo.org/gdal/<wbr>changeset/38405</a></p>
<p style="margin:0px;text-indent:0px">> </p>
<p style="margin:0px;text-indent:0px">> Exactly why is it okay to resize this fixed size structure?</p>
<p style="margin:0px;text-indent:0px"> </p>
</span><p style="margin:0px;text-indent:0px">Well, this is a good old trick of grey-beard C programmers, isn't it ? Not sure what the C/C++ standards says about that, but it works pretty well in practice.</p>
<p style="margin:0px;text-indent:0px"> </p>
<p style="margin:0px;text-indent:0px">Some explanations at:</p>
<p style="margin:0px;text-indent:0px"><a href="http://stackoverflow.com/a/3123792" target="_blank">http://stackoverflow.com/a/<wbr>3123792</a></p>
<p style="margin:0px;text-indent:0px"> </p>
<p style="margin:0px;text-indent:0px">-- </p>
<p style="margin:0px;text-indent:0px">Spatialys - Geospatial professional services</p>
<p style="margin:0px;text-indent:0px"><a href="http://www.spatialys.com" target="_blank">http://www.spatialys.com</a></p></div></blockquote></div><br><br clear="all"><div><br></div><div class="gmail_signature"></div>
</div></div>