可变长度与Malloc数组 [英] Variable Length vs Malloc-ed arrays

查看:145
本文介绍了可变长度与Malloc数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据c99标准,我们可以编写以下代码,并且完全合法

According to c99 standard, we can write the following code and it's totally legal

int x;
scanf("%d",&x);
int ar[x];

我的问题是,如果我可以分配这样的数组,为什么还要用malloc再次分配可变大小的数组?

My question is, if I can allocate an array like this, why would I ever need malloc to allocate variable size arrays again?

另外,您能否解释一下可变长度数组的分配是如何发生的?在内部深处,它调用malloc来分配数组还是什么?

Also, could you please explain how does the variable length arrays allocation happens? Deep inside, does it call malloc to allocate the array or what?

推荐答案

我想到两个原因:

  1. 存在于此堆栈框架之外的阵列.
  2. 大于堆栈的数组.

这篇关于可变长度与Malloc数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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