gets into char *str without malloc, no segfault?

后端 未结 2 1136
我在风中等你
我在风中等你 2021-01-28 22:41

I\'ve just compiled this C program using Cygwin\'s gcc:

#include 

void main (){
    char *str;
    gets(str);
    printf(\"%s\",str);
}
         


        
2条回答
  •  猫巷女王i
    2021-01-28 23:31

    Access memory region pointed to by uninitialized pointer is undefined behavior, it could crash, it also could look like working normally. In a word, you cannot predict its behavior.

    How come I'm not getting a segmentation fault?

    Uninitialized pointer has an undetermined value, it could point to anywhere, if it points to some big enough writable region accidently, that program will "work" normally.

提交回复
热议问题