Why does this intentionally incorrect use of strcpy not fail horribly?

后端 未结 5 536
鱼传尺愫
鱼传尺愫 2020-11-29 09:28

Why does the below C code using strcpy work just fine for me? I tried to make it fail in two ways:

1) I tried strcpy from a string literal

5条回答
  •  半阙折子戏
    2020-11-29 10:28

    As @Als said, this is undefined behaviour. This may crash, but it doesn't have to.

    Many memory managers allocate in larger chunks of memory and then hand it to the "user" in smaller chunks, probably a mutliple of 4 or 8 bytes. So your write over the boundary probably simply writes into the extra bytes allocated. Or it overwrites one of the other variables you have.

提交回复
热议问题