Dereferencing type-punned pointer will break strict-aliasing rules

后端 未结 7 1460
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 01:05

I used the following piece of code to read data from files as part of a larger program.

double data_read(FILE *stream,int code) {
        char data[8];
              


        
7条回答
  •  攒了一身酷
    2020-12-01 01:31

    Basically you can read gcc's message as guy you are looking for trouble, don't say I didn't warn ya.

    Casting a three byte character array to an int is one of the worst things I have seen, ever. Normally your int has at least 4 bytes. So for the fourth (and maybe more if int is wider) you get random data. And then you cast all of this to a double.

    Just do none of that. The aliasing problem that gcc warns about is innocent compared to what you are doing.

提交回复
热议问题