Why Segmentation fault in following code?

后端 未结 5 1230
忘了有多久
忘了有多久 2021-01-21 14:18

I read this on wikipedia

    int main(void)
 {

    char *s = \"hello world\";
    *s = \'H\';

 }

When the program containing this code is com

5条回答
  •  再見小時候
    2021-01-21 14:28

    Per the language definition, string literals have to be stored in such a way that their lifetime extends over the lifetime of the program, and that they are visible over the entire program.

    Exactly what this means in terms of where the string gets stored is up to the implementation; the language definition does not mandate that string literals are stored in read-only memory, and not all implementations do so. It only says that attempting to modify the contents of a string literal results in undefined behavior, meaning the implementation is free to do whatever it wants.

提交回复
热议问题