mardi 18 novembre 2014

Does this really break strict-aliasing rules?


Vote count:

0




When I compile this sample code using g++, I get this warning:



test.cpp:8:48: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]


The code:



#include <iostream>

int main(int argc, char ** argv)
{
alignas(int) char data[sizeof(int)];
int *myInt = new(data) int;
*myInt = 34;

std::cout << *reinterpret_cast<int*>(data) << std::endl;
}


In this case, doesn't data alias an int, and therefore casting it back to an int would not violate strict aliasing rules? Or am I missing something here?



asked 1 min ago

Red Alert

1,877






Does this really break strict-aliasing rules?

Aucun commentaire:

Enregistrer un commentaire