[C99]本地VLA的维度由全局变量给出? [英] [C99] local VLA with dimension given by a global var?

查看:62
本文介绍了[C99]本地VLA的维度由全局变量给出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究的系统支持C99的一个子集,其中

符合标准的VLA。


我已经据悉,VLA不具备全球范围。我的问题是

我是否可以安全地声明一个(本地)VLA,使其具有类型为int的

全局变量:


短暂的昏暗;


int main(无效)

{


/ *(... ),可能访问/更改昏暗的值* / $

{

int vla [dim];

}


/ *(...)* /


返回0;


}


如果我理解了编译器工作原理的不同基础知识这是一个很简单的问题,即在上面的代码中变量dim就是

在那一点进行评估,并且在该时间点*的值*是用于定义数组维度的
;它是否正确?也就是说,我可以安全吗?$ / b
$ b谢谢你,
>

The system I am working on supports a subset of C99, among which
"standard-compliant VLAs".

I''ve already learnt that VLAs can''t have global scope. My question is
whether I can safely declare a (local) VLA to have as its dimension a
global variable of type int:

short dim;

int main(void)
{

/* (...), possibly access/change value of dim */

{
int vla[dim];
}

/* (...) */

return 0;

}

If I understand the vary basics of how the compiler works this is a
trivial question, ie, in the code above the variable dim is just being
evaluated at that point and its value *at that point in time* is being
used to define the dimension of the array; is this correct? Ie, can I
safely this?

Thank you,

Mack

推荐答案



" MackS" <毫安*********** @ hotmail.com>在消息中写道

news:11 ********************** @ c13g2000cwb.googlegr oups.com ...

"MackS" <ma***********@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
我正在研究的系统支持C99的一个子集,其中包括符合标准的VLA。

我已经知道VLA不能具有全球范围。我的问题是
我是否可以安全地声明一个(本地)VLA使其尺寸为int类型的全局变量:

短昏暗;

int main(void)
{

/ *(...),可能访问/更改dim的值* /

int vla [dim];
}

/ *(...)* /

返回0;

}
谢谢你,

Mack
The system I am working on supports a subset of C99, among which
"standard-compliant VLAs".

I''ve already learnt that VLAs can''t have global scope. My question is
whether I can safely declare a (local) VLA to have as its dimension a
global variable of type int:

short dim;

int main(void)
{

/* (...), possibly access/change value of dim */

{
int vla[dim];
}

/* (...) */

return 0;

}

If I understand the vary basics of how the compiler works this is a
trivial question, ie, in the code above the variable dim is just being
evaluated at that point and its value *at that point in time* is being
used to define the dimension of the array; is this correct? Ie, can I
safely this?

Thank you,

Mack




是的,这个很好。 6.7.5.2#9说明了这一点。


-

j



Yes, this is perfectly fine. 6.7.5.2#9 illustrates this.

--
j


2005年2月19日09 :06:40-0800,MackS < ma *********** @ hotmail.com>

在comp.lang.c中写道:
On 19 Feb 2005 09:06:40 -0800, "MackS" <ma***********@hotmail.com>
wrote in comp.lang.c:
系统I我正致力于支持C99的一个子集,其中包括符合标准的VLA。

我已经知道,VLA不具备全局范围。我的问题是


它与范围无关,它与存储持续时间有关。

VLA必须具有自动存储持续时间,并且可以'' t具有静态

存储持续时间。

是否可以安全地声明(本地)VLA使其尺寸为int类型的全局变量:
int main(无效)
{

/ *(...),可能访问/更改昏暗的值* /

{/ dim];
}
/ *(...)* /
return 0;


如果我理解编译器如何工作的各种基础知识这是一个简单的问题,即在变量上面的代码中dim只是在那时被评估,它在那个时间点的值*被用来定义数组的维度;它是否正确?即,我可以安全吗?
The system I am working on supports a subset of C99, among which
"standard-compliant VLAs".

I''ve already learnt that VLAs can''t have global scope. My question is
It has nothing to do with scope, it has to do with storage duration.
VLAs must have automatic storage duration, and can''t have static
storage duration.
whether I can safely declare a (local) VLA to have as its dimension a
global variable of type int:

short dim;

int main(void)
{

/* (...), possibly access/change value of dim */

{
int vla[dim];
}

/* (...) */

return 0;

}

If I understand the vary basics of how the compiler works this is a
trivial question, ie, in the code above the variable dim is just being
evaluated at that point and its value *at that point in time* is being
used to define the dimension of the array; is this correct? Ie, can I
safely this?




是的,VLA的大小将是表达式

评估的值在创建数组的时候。


-

Jack Klein

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

常见问题解答

comp.lang.c http://www.eskimo。 com /~scs / C-faq / top.html

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



Yes, the size of the VLA will be the value that the expression
evaluates to at the time the array is created.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
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


2005-02-19,Jack Klein< ja*******@spamcop.net>写道:
On 2005-02-19, Jack Klein <ja*******@spamcop.net> wrote:

它与范围无关,它与存储持续时间有关。
VLA必须具有自动存储持续时间,并且不能具有静态
存储持续时间。

It has nothing to do with scope, it has to do with storage duration.
VLAs must have automatic storage duration, and can''t have static
storage duration.



说到VLA ..

看来该标准没有具体规定如何实现VLA,尽管

最明显的方法是:

1.堆栈(如非便携式alloca())

2. malloc()上堆


在这两种情况下,您都可以声明一个荒谬的大型VLA,这样就没有足够的内存来分配它。我还没有在C99标准中找到什么

然后发生..


那么标准的处理方式是什么? VGA的内存不足状态?

没有地方可以像'malloc()那样''返回NULL'。


Speaking of VLAs..

It seems that the standard doesn''t specify how VLAs are implemented, although
the most obvious ways are:
1. stack (like the non-portable alloca())
2. malloc() on the heap

In both cases you can declare an absurdly large VLA so that there isn''t enough
memory to allocate it. I haven''t been able to find in the C99 standard what
happens then..

So what is the ''standard'' way to handle an out-of-memory condition for VLAs?
There is no place to ''return NULL'' as malloc() does..


这篇关于[C99]本地VLA的维度由全局变量给出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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