-1
In file included from /usr/local/Qt/linux-g++/include/QtCore/QLinkedList:2,
  from /home/bamboo/Packages/Parser.h:17,
  from /home/bamboo/Packages/Module.cpp:6:
/usr/local/Qt/linux-g++/include/QtCore/qlinkedlist.h: In member function 'void QLinkedList<T>::clear() [with T = int]':
/usr/local/Qt/linux-g++/include/QtCore/qlinkedlist.h:294: error: dereferencing pointer 'y' does break strict-aliasing rules
/usr/local/Qt/linux-g++/include/QtCore/qlinkedlist.h:293: note: initialized from here
/usr/local/Qt/linux-g++/include/QtCore/qlinkedlist.h:294: error: dereferencing pointer 'y' does break strict-aliasing rules
/usr/local/Qt/linux-g++/include/QtCore/qlinkedlist.h:293: note: initialized from here

where in big lines class Module contains a member template like: Parser<int> and the class parser is defined:

template <typename T> class Parser
{
   // some stuff
   QLinkedList<T> stuff;
};

and this piece of code compiles nicely with gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3 and nicely with g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2 and fails with g++ (Debian 4.4.5-8) 4.4.5 ... and I have no idea why? Anyone has seen this error message and anyone know what this might mean?... and more importantly how to solve it?

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • additonally this article does pretty good describe it: http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html – dhein Nov 28 '13 at 13:46
  • Yup, I saw that post before posting this question, and the Qt header is actually using that technique ... So this must be something different. – Ferenc Deak Nov 28 '13 at 13:51
  • You may get better help if you make a http://sscce.org so others can compile it and poke at it. What Qt version btw? – Yakk - Adam Nevraumont Nov 28 '13 at 14:28

1 Answers1

0

Aliasing means that a pointer int *i points to the same address as double *d. So if

int i = 5;
int *pi = &i:
double *d = pi;

here d is aliasing pi.

this is in c99 >illegal<

I don't know how exactly c++ treats it, but I can't imagine it is welcome.

If you want to test a code where it will be getting funny, try this code in different optimisation levels.

You will get differen results with gcc 4.2

uint32_t anint;

int main(int arg, char** argv)
{
    foo ((uint64_t *)&anint);
    return 0;
}

void foo (uint64_t *dblptr)
{
    anint = 88;
    *dblptr = 86;
    dosmthng (anint);
}

void dosmthng (uint32_t val)
{
    printf ("%d\r\n", val);
}

if you do -O2 or higher the output will be 88. because the compiler expects you to respect the strict-aliasing rule and expects *dblptr as never been used in this code, and jsut takes the line out.

If you any way see no way of working wihtout aliasing, you can give the compiler the param -fno-strict-aliasing. This forces GCC to do not any optimisation based on this expection.

But anyway in C it is not strict ISO C code if you do wrong type punning.

(So if it may ease you, a lot of C code on famous programms gets compiled with -fno-strict-aliasing)

dhein
  • 6,431
  • 4
  • 42
  • 74