[gdal-dev] Resizing CPLErrorContext?

Kurt Schwehr schwehr at gmail.com
Tue May 16 10:26:09 PDT 2017


I now know this is referred to as the "struct hack."

This really is undefined behavior in the C++11 standard...  there was a
proposal to make the struct hack valid for C++11:

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n791.htm
<https://www.google.com/url?sa=D&q=http%3A%2F%2Fwww.open-std.org%2Fjtc1%2Fsc22%2Fwg14%2Fwww%2Fdocs%2Fn791.htm>

>From another google engineer:

5.2.1 tells you that x[y] is identical to *(x + y).
5.7.5 tells you that x + y has undefined behavior if the result points
outside of the array.

>From the C++11 standard:

5.2.1 Subscripting [expr.sub]
1 A postfix expression followed by an expression in square brackets is a
postfix expression. One of the expressions
shall have the type “pointer to T” and the other shall have unscoped
enumeration or integral type.
The result is an lvalue of type “T.” The type “T” shall be a
completely-defined object type.62 The expression
E1[E2] is identical (by definition) to *((E1)+(E2)) [ Note: see 5.3 and 5.7
for details of * and + and 8.3.4
for details of arrays. — end note ]

5.7 Additive operators
...

5.7.5 When an expression that has integral type is added to or subtracted
from a pointer, the result has the type
of the pointer operand. If the pointer operand points to an element of an
array object, and the array is
large enough, the result points to an element offset from the original
element such that the difference of
the subscripts of the resulting and original array elements equals the
integral expression. In other words, if
the expression P points to the i-th element of an array object, the
expressions (P)+N (equivalently, N+(P))
and (P)-N (where N has the value n) point to, respectively, the i + n-th
and i − n-th elements of the array
object, provided they exist. Moreover, if the expression P points to the
last element of an array object,
the expression (P)+1 points one past the last element of the array object,
and if the expression Q points
one past the last element of an array object, the expression (Q)-1 points
to the last element of the array
object. If both the pointer operand and the result point to elements of the
same array object, or one past
the last element of the array object, the evaluation shall not produce an
overflow; otherwise, the behavior is
undefined.



On Tue, May 16, 2017 at 7:35 AM, Even Rouault <even.rouault at spatialys.com>
wrote:

> On mardi 16 mai 2017 07:01:53 CEST Kurt Schwehr wrote:
>
> > w.r.t. https://trac.osgeo.org/gdal/changeset/38405
>
> >
>
> > Exactly why is it okay to resize this fixed size structure?
>
>
>
> 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.
>
>
>
> Some explanations at:
>
> http://stackoverflow.com/a/3123792
>
>
>
> --
>
> Spatialys - Geospatial professional services
>
> http://www.spatialys.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20170516/9c658766/attachment.html>


More information about the gdal-dev mailing list