什么是的alloca(n)和焦X [n]的区别? [英] What's the difference between alloca(n) and char x[n]?

查看:198
本文介绍了什么是的alloca(n)和焦X [n]的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么

void *bytes = alloca(size);

char bytes[size];  //Or to be more precise, char x[size]; void *bytes = x;

...其中的尺寸的是一个变量,其值是在编译时未知的。

...where size is a variable whose value is unknown at compile-time.

推荐答案

的alloca(),直到当前函数结束不回收内存,而可变长数组将收回在当前段结束的记忆。

alloca() does not reclaim memory until the current function ends, while the variable length array reclaims the memory when the current block ends.

换句话说:

void foo()
{
    size_t size = 42;
    if (size) {
        void *bytes1 = alloca(size);
        char bytes2[size];
    } // bytes2 is deallocated here
}; //bytes1 is deallocated here

的alloca()可支持(在一个时尚)上的任何C89编译器,而可变长度阵列需要一个C99编译。

alloca() can be supported (in a fashion) on any C89 compiler, while the variable length array requires a C99 compiler.

这篇关于什么是的alloca(n)和焦X [n]的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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