[Qt-interest] dereferencing pointer '<anonymous>' does break strict-aliasing rules error
Anton Chernov
mechernov at gmail.com
Wed May 11 14:16:35 CEST 2011
Seems an interesting question, i didn't get it quite well, did you meaned
int i = 42;
ushort* u = (ushort *)&i;
*u = 43;
? And what can went wrong here? Or was it a joke, right?
2011/5/11 Thiago Macieira <thiago at kde.org>
> On Wednesday, 11 de May de 2011 12:29:45 Konrad Rosenbaum wrote:
> > On Tuesday 10 May 2011 14:38:23 mmiacca wrote:
> > > While i try with Qt 2010.05 (Qt 4.7.0 32 bits Windows) to compile
> > > C:\Qt\2010.05\qt\examples\qtestlib\tutorial4 i get this error
> > > ..\..\..\include/QtTest/../../src/testlib/qtestspontaneevent.h:77:
> > > warning: dereferencing pointer '<anonymous>' does break strict-aliasing
> > > rules
> >
> > It's just a warning. Unless there is more (i.e. a real error) the
> > application should still build and be executable.
>
> That doesn't mean it will work right. Then again, there's warning-free code
> that doesn't work right either :-)
>
> The C and C++ languages forbid aliasing of types (except for aliasing with
> char). So the following code is malformed:
>
> int i = 42;
> ushort u = (ushort *)&i;
> u = 43;
>
> printf("%d", i);
>
> The types "i" and "u" alias each other and this is not allowed.
>
> Undefined behaviour means anything could happen. The possible outcomes are
> (assuming little-endian machine):
>
> 1) it prints "43" because it did modify the memory located by i.
>
> 2) it prints "42" because the compiler concludes that the variable i didn't
> get modified between its assignment and its use. The two intermediary lines
> are
> considered dead code and eliminated.
>
> 3) it calls your ex-mother-in-law and gives her your new girlfriend's phone
> number.
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Senior Product Manager - Nokia, Qt Development Frameworks
> PGP/GPG: 0x6EF45358; fingerprint:
> E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110511/27bd2c9c/attachment.html
More information about the Qt-interest-old
mailing list