当我只能定义一个可变长度数组时,为什么要使用malloc()? [英] Why use malloc() when I can just define a variable-length array?

查看:218
本文介绍了当我只能定义一个可变长度数组时,为什么要使用malloc()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关在C中动态创建数组的知识.因此,那里描述的正确方法是:

I was reading about creating array dynamically in C. So the proper way as described there was:

int n;
scanf("%d", &n);
int *arr = (int*)malloc(n*sizeof(int));

但是后来我想我是否可以做这样的事情-

But then I thought if I could just do something like this-

int n, i, sum=0;
scanf("%d", &n);
int arr[n];

我编译并运行了它,没有任何错误.所以,我的问题是为什么我应该使用malloc()?这与新旧C版本有关吗?

And I compiled and ran it without any error. So, my question is why should I use malloc()? Does this have something to do with the old and new C versions?

推荐答案

在可变长度数组上使用malloc至少有五个好处.

There are at least five benefits to using malloc over variable length arrays.

  1. 最值得注意的是,使用malloc创建的对象在当前块的执行结束后仍然存在.这意味着可以将这些对象(通过指针)返回给函数的调用者.在实际应用中,这种用法很常见.创建为可变长度数组的数组在其块执行结束时不复存在.

  1. Most notably, objects created with malloc persist after execution of the current block ends. This means that such objects can be returned (by pointer) to callers of functions. This use is frequent in real-world applications. Arrays created as variable-length arrays cease to exist when execution of their block ends.

使用malloc创建的数组可以使用realloc调整大小.可变长度数组无法调整大小.

Arrays created with malloc can be resized with realloc. Variable-length arrays cannot be resized.

从2011 C标准开始,对于C实现,可变长度数组是可选的.任何质量的通用C实现都将支持它们,但是事实是它们是可选的,意味着要移植的代码必须不使用可变长度数组,或者必须通过测试预处理器宏并提供备用代码.

As of the 2011 C standard, variable-length arrays are optional for C implementations to support. A general-purpose C implementation of any quality will support them, but the fact they are optional means code that is intended to be portable must either not use variable-length arrays or must guard against the lack of support by testing the preprocessor macro __STDC_NO_VLA__ and providing alternate code.

常见的可变长度数组的大小限制比malloc分配的数组大.可变长度数组通常使用堆栈空间来实现,并且堆栈通常被限制为不大一些的兆字节(尽管在构建可执行文件时通常可以增加字节数).对于用malloc创建的对象,在现代系统中可能有千兆字节的内存.

Commonly, variable-length arrays are much more limited in size than arrays allocated with malloc. Variable-length arrays are generally implemented using stack space, and stacks are typically limited to some not-large number of mebibytes (although that can generally be increased when building an executable). For objects created with malloc, gibibytes of memory may be available in modern systems.

如果创建数组确实失败并显示malloc,则将返回NULL,程序员可以轻松编写代码以检测并对其进行处理.如果创建可变长度数组失败,则常见的行为是操作系统因内存错误而终止程序. (各种C实现可能提供了拦截此错误的方法,但比测试NULLmalloc返回值要麻烦得多,并且它不是可移植的.)

If creation of an array does fail with malloc, NULL will be returned, and the programmer can easily write code to detect that and deal with it. If creation of a variable-length array fails, the common behavior is for the operating system to terminate the program with some memory error. (Various C implementations may provide means to intercept this error, but it is considerably more of a nuisance than testing the malloc return value for NULL, and it is not portable.)

这篇关于当我只能定义一个可变长度数组时,为什么要使用malloc()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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