为什么在strcpy上没有分段错误? [英] Why no segmentation fault on strcpy?

查看:63
本文介绍了为什么在strcpy上没有分段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

未定义,未指定和实现定义的行为

这应该隔离故障。为什么呢?

This should seg fault. Why doesn't it.

    #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;
    }

我正在使用带有以下命令的gcc版本4.4.3:

I am using gcc version 4.4.3 with the following command:

    gcc -std=c99 testString.c -o test

我还尝试将优化设置为o(-O0)。

I also tried setting optimisation to o (-O0).

推荐答案


这应该隔离故障

This should seg fault

没有理由应该隔离故障。代码的行为是不确定的。这并不意味着它一定要崩溃。

There's no reason it "should" segfault. The behaviour of the code is undefined. This does not mean it necessarily has to crash.

这篇关于为什么在strcpy上没有分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆