c++ - gcc optimization flag break code -
this code works fine when no optimization flag set:
#include <cstdio> int main(){ float *ptr = ({float var[10] = {1,2,3,4,5,6,7,8,9,10}; var;}); float *ptr1 = ({float var[10]; for(int i_=0;i_<10;i_++)var[i_]=i_+1; var;}); float *ptr2 = ({float var[10]; var[4]=5; var;}); printf("\n value = %f %f %f",ptr[4],ptr1[4],ptr2[4]); }
it returns 5 5 5
expected. when optimization flag set, returns 5 0 5
.
ptr1
have problem related loop. why? maybe bug?
i'm using latest, 4.8.0, tested x64, x86 other builds. same behaviour.
you're using gnu language extension, let's @ gnu documentation:
in statement expression, temporaries created within statement destroyed @ statement's end.
var
destroyed every time, , pointer first element (which what's returned expressions) not safe dereference.
Comments
Post a Comment