Mis-aligned pointers on x86

后端 未结 8 660
一整个雨季
一整个雨季 2020-11-30 06:36

Can someone provide an example were casting a pointer from one type to another fails due to mis-alignment?

In the comments to this answer, bothie states that doing s

8条回答
  •  半阙折子戏
    2020-11-30 07:21

    char *foo = "....";
    foo++;
    int *bar = (int *)foo;
    

    The compiler would put foo on a word boundary, and then when you increment it it's at a word+1, which is invalid for a int pointer.

提交回复
热议问题