为什么strcpy没有分段错误?

sie*_*nko 3 c stack-overflow c99 strcpy

可能重复:
未定义,未指定和实现定义的行为

这应该是错误的.为什么不呢.

    #include <string.h>
    #include <stdio.h>

    char str1[] = "Sample string. Sample string. Sample string. Sample string. Sample string. ";
    char str2[2];

    int main ()
    {
      strcpy (str2,str1);
      printf("%s\n", str2);
      return 0;
    }
Run Code Online (Sandbox Code Playgroud)

我使用gcc版本4.4.3与以下命令:

    gcc -std=c99 testString.c -o test
Run Code Online (Sandbox Code Playgroud)

我也尝试将优化设置为o(-O0).

NPE*_*NPE 8

这应该是错误的

它没有理由"应该"段错误.代码的行为是未定义的.这并不意味着它必然会崩溃.