普通数组也是动态的吗? [英] Are normal Arrays also dynamic?

查看:40
本文介绍了普通数组也是动态的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在C语言中的代码

The following is my code in C

main(){

    int a[1];
    a[0]=10;
    a[1]=12;
    printf("%d\n",a[1]);
    copy_arr(a);
    printf("%d",a[1]);
}

以下是输出

12
12

它不应该给数组提供界外或类似的东西吗?

Shouldn't it give array out of bound or something like this?

还是上面的代码取决于编译器?

Or does the code above depend on the compiler?

我正在gcc编译器中运行以上代码.

I am running the above in a gcc compiler.

推荐答案

C中没有自动检查数组边界的方法.您的代码具有未定义的行为(UB).这意味着它有时似乎起作用".但是您不能依靠任何特定的结果.具有UB的程序是损坏的程序.

There's no automatic array bounds checking in C. Your code has undefined behaviour (UB). This means it can sometimes seem to "work". But you can't rely on any particular outcome. A program with UB is a broken program.

这篇关于普通数组也是动态的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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