C99与malloc的VLA特征 [英] VLA feature of C99 vs malloc

查看:91
本文介绍了C99与malloc的VLA特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设

int size = INT_MAX / 8;

char a [size];

声明使用变量lenght的功能C99中的数组(VLA)。

使用适当的#includes,当运行此代码时,使用32位编译器的Red Hat企业Linux中会发生分段错误

。 />

如果

char * str =(char *)malloc(INT_MAX);

被声明并运行,并带有适当的标题, malloc能够

返回INT_MAX字节。(这是以前VLA的8倍

声明)


这是否对于大量内存分配,malloc应该优先于可变长度

数组?

标准是否指定了有关VLA大小的任何限制。

Suppose
int size = INT_MAX / 8;
char a[size];
are declared to use the feature of variable lenght array(VLA) in C99.
With appropriate #includes, when this code is run, segmentation fault
occurs in Red Hat enterprise Linux with 32-bit compiler.

Howver if
char * str = (char *)malloc(INT_MAX);
is declared and run , with appropriate headers, malloc is able to
return INT_MAX bytes.(this is 8 times more than the previous VLA
declaration)

Does this mean that malloc should be preferred over variable length
arrays, for huge memory allocations ?
Does the standard specify any limit regarding the size of VLA.

推荐答案

Le 18-12-2006,subramanian< su ************** @yahoo.comaécrit* :
Le 18-12-2006, subramanian <su**************@yahoo.coma écrit*:

假设

int size = INT_MAX / 8;

c har [size];

声明在C99中使用变量长度数组(VLA)的特性。

使用适当的#includes,当运行此代码时,分割故障

发生在使用32位编译器的Red Hat企业Linux中。


如果

char * str =(char * )malloc(INT_MAX);

被声明并运行,带有适当的标题,malloc能够返回INT_MAX字节。(这是前一个VLA的8倍

声明)


这是否意味着malloc应该优先于可变长度的
数组,以获得巨大的内存分配?
Suppose
int size = INT_MAX / 8;
char a[size];
are declared to use the feature of variable lenght array(VLA) in C99.
With appropriate #includes, when this code is run, segmentation fault
occurs in Red Hat enterprise Linux with 32-bit compiler.

Howver if
char * str = (char *)malloc(INT_MAX);
is declared and run , with appropriate headers, malloc is able to
return INT_MAX bytes.(this is 8 times more than the previous VLA
declaration)

Does this mean that malloc should be preferred over variable length
arrays, for huge memory allocations ?



此限制是特定于平台的,而C99没有声明

所以。但是,在许多平台中,您可以发现这种限制。

This limitation is platform-specific, and C99 does not states
so. But, in many platform, you can found this kind of limitations.


标准是否指定了有关VLA大小的任何限制。
Does the standard specify any limit regarding the size of VLA.



据我所知。


Marc Boyer

Not to my knowledge.

Marc Boyer




2006年12月18日星期一,subramanian写道:

On Mon, 18 Dec 2006, subramanian wrote:

假设

int size = INT_MAX / 8;

char a [size];

声明在C99中使用变量长度数组(VLA)的功能。

使用适当的#includes ,当运行此代码时,分段错误

发生在使用32位编译器的Red Hat企业Linux中。


如果

char * str =(char *)malloc(INT_MAX);

被声明并运行,使用适当的头,malloc能够

返回INT_MAX字节。(这是8倍于之前的VLA

声明)


这是否意味着malloc应该优先于可变长度

数组,用于巨大的内存分配?
Suppose
int size = INT_MAX / 8;
char a[size];
are declared to use the feature of variable lenght array(VLA) in C99.
With appropriate #includes, when this code is run, segmentation fault
occurs in Red Hat enterprise Linux with 32-bit compiler.

Howver if
char * str = (char *)malloc(INT_MAX);
is declared and run , with appropriate headers, malloc is able to
return INT_MAX bytes.(this is 8 times more than the previous VLA
declaration)

Does this mean that malloc should be preferred over variable length
arrays, for huge memory allocations ?



我认为你的问题与VLA几乎没有关系。你的问题是你

分配一个大的自动对象。最有可能用一个

普通自动C阵列替换VLA,例如:


char a [INT_MAX / 8];


将产生同样的问题。一般情况下,自动对象在堆栈上分配,而平台会对

堆栈的大小进行各种限制。例如,Windows的上限为1兆字节,linux的内核模式堆栈限制为8或4千字节。此外,一些

系统管理员对堆栈大小设置了每个进程的限制。底部

行:如果你想分配大型对象,请使用* alloc(),而不是自动

局部变量。


Emil

I think your problem has little to do with VLAs. Your problem is that you
allocate a large automatic object. Most likely replacing the VLA with a
normal automatic C array such as:

char a[INT_MAX/8];

will yield the same problem. In general automatic objects are allocated on
the stack, and platforms place various retrictions on the size of the
stacks. For example Windows places an upper limit of 1 megabyte, the
kernel-mode stack of linux is limited to 8 or 4 kilobytes. Moreover, some
system administrators place per-process limits on the stack size. Bottom
line: If you want to allocate large objects, use *alloc(), not automatic
local variables.

Emil


标准是否指定了有关VLA大小的任何限制。

Does the standard specify any limit regarding the size of VLA.

< br>

2006年12月18日01:24:25 -0800,subramanian

< su ************** @ yahoo。在comp.lang.c中写了:
On 18 Dec 2006 01:24:25 -0800, "subramanian"
<su**************@yahoo.comwrote in comp.lang.c:

假设

int size = INT_MAX / 8;

char a [size];

声明在C99中使用变量长度数组(VLA)的特性。

使用适当的#includes,当运行此代码时,分段错误

出现在带有32位编译器的Red Hat企业Linux中。

如果

char * str =(char *)malloc (INT_MAX);

被声明并运行,带有适当的头,malloc能够

返回INT_MAX字节。(这比前面的8倍多) vious vla

声明)


这是否意味着malloc应该优先于可变长度的
数组,以获得巨大的内存分配?

标准是否规定了有关VLA大小的任何限制。
Suppose
int size = INT_MAX / 8;
char a[size];
are declared to use the feature of variable lenght array(VLA) in C99.
With appropriate #includes, when this code is run, segmentation fault
occurs in Red Hat enterprise Linux with 32-bit compiler.

Howver if
char * str = (char *)malloc(INT_MAX);
is declared and run , with appropriate headers, malloc is able to
return INT_MAX bytes.(this is 8 times more than the previous VLA
declaration)

Does this mean that malloc should be preferred over variable length
arrays, for huge memory allocations ?
Does the standard specify any limit regarding the size of VLA.



VGA是由C99添加的无用且不安全的功能,除非你知道

它们总是非常小。


他们是那些非标准扩展的狂热用户的骨头

喜欢alloca(),他坚持认为他们的程序不可能存活

真正的内存分配开销。


就像它取代的扩展一样,VLA也遇到同样的问题。

它们受限于环境自动大小的限制

分配。


为了证明这一点,请将定义大小的行替换为int

,宏将其定义为相同的值。如果程序因为你的实现不允许你自动

分配那么大,那么它将失败并且VLA会失败。

唯一的区别是你可以验证malloc()及其

亲属是成功还是失败,但在许多平台上分配太大

a VLA导致立即终止计划。


-

Jack Klein

主页: http://JK-Technology.Com



comp.lang.c的常见问题解答 http://c-faq.com/

comp .lang.c ++ http://www.parashift.com/c++-faq -lite /

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a。 ..FAQ- acllc.html

VLAs are a useless and unsafe feature added by C99, unless you know
they are always going to be very small.

They are a bone thrown to those rabid users of non-standard extensions
like alloca(), who insist that their programs can''t possibly survive
the overhead of true memory allocation.

Like the extensions it replaces, VLAs suffer from the same problem.
They are limited by the environment''s bounds on the size of automatic
allocation.

To prove this to your self, replace the line defining size as an int
with a macro defining it to the same value. If the program fails with
the macro because you implementation does not allow you an automatic
allocation that large, it will fail with the VLA.

The only difference is that you can verify whether malloc() and its
relatives succeed or fail, but on many platforms allocating too large
a VLA results in immediate program termination.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于C99与malloc的VLA特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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