GNU编译器与数组上的Visual Studio分配w /范围内的长度常量 [英] GNU Compilers vs. Visual Studio on Arrays Allocated w/ Length Constant w/in a Scope

查看:240
本文介绍了GNU编译器与数组上的Visual Studio分配w /范围内的长度常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果您在c / c ++中设置了动态值,则不能在括号内使用该值来分配数组(这将使其成为所谓的可变长度数组(VLA),其中< a href =http://www.stroustrup.com/C++11FAQ.html =nofollow noreferrer>当前的C ++标准不支持)
ie请参阅:
C ++:可变长度数组

http://en.wikipedia.org/wiki/Variable- length_array _



我不太明白(而且我没看到在这里询问)是为什么GNU c / c ++编译器使用基于整数值的动态分配(据我所知),可以使用动态分配( gcc g ++ 只要该值在数组分配的范围内是一个常量,但 Visual Studio 不支持这个和w生病的拒绝编译代码,吐出错误。



g ++

  void Foo(const unsigned int bar)
{
double myStuff [bar];
// ...做东西...
}

。编译只是很好...



但同样的代码拒绝在我使用的VS版本中编译,除非我传递给bar是否为所有范围中的const ,或者是 #define static const 等。 / p>

我怀疑GNU编译器可能会使用该范围来推断该值是该范围内的一个常量,也可以简单地将其分配给一个malloc或者以某种方式处理它。



我的问题是:


  1. 谁(VS或GNU)关于他们如何处理这个
    的标准?

  2. 有没有办法使用 [] 在范围内不变但不是全局的值 const 整个程序没有一个 malloc call?

  3. 有什么问题我应该知道我是否在我的
    GNU编译代码中使用这个?


解决方案

语言的角度来看,VLA仅在C中支持,只有C99才支持。它们在C ++中不受支持。



从编译器的角度来看,g ++将支持VLA作为扩展到C90和C ++,但如果使用 -pedantic 编译,它将禁用这些扩展,您将收到编译错误。



Visual Studio不支持C或C ++中的所有的VLAs。 VS只支持达到C89标准,AFAIK MS没有计划支持以后的C标准。



就范围而言,这在 C标准


6.7.6.2数组声明符

...

2如果标识符被声明为具有可变修改类型,则它应为普通的
标识符(如6.2.3中所定义)没有链接,并且具有块范围或函数
原型范围。如果标识符被声明为具有静态或线程存储
持续时间的对象,则不会有可变长度的数组类型。

有些技术原因是为什么VLAs无法声明为 static 或文件范围;具有静态存储持续时间的对象在程序启动时被分配,并保持到程序终止,如果我没有被误认为不能保证对象将以任何特定顺序被分配和初始化。所以这些项目需要在编译时才知道它们的大小。


I'm aware that if you set a dynamic value in c/c++ you can't use that value within brackets to allocate an array (which would make it a so-called variable length array (VLA), which the current C++ standard does not support)...

i.e. See:
C++ : Variable Length Array
http://en.wikipedia.org/wiki/Variable-length_array

What I don't quite get (and what I haven't see asked precisely here) is why GNU c/c++ compilers (gcc, g++) are okay with using dynamic allocation based on an integer value (as far as I can tell) so long as that value is a constant within the scope of the array allocation, but Visual Studio's does not support this and will refuse to compile the code, spitting out errors.

e.g. in g++

void Foo(const unsigned int bar)
{
  double myStuff[bar];
  //... do stuff...
}

...compiles just fine...

But the same code refuses to compile in versions of VS I've used, unless whatever I pass to bar is const in all scopes or is a #define, static const, etc.

I would suspect that maybe GNU compilers use the scope to infer that this value is a constant within that scope and either simply assign it to a malloc or handle it specially somehow.

My questions are:

  1. Who (VS or GNU) is closer to the standard in terms of how they handle this?
  2. Is there a way to do this VS using [] on a value that's constant within scope, but not globally const throughout the entire program without a malloc call?
  3. Are there any issues I should be aware of if I use this in my GNU-compiled code?

解决方案

From a language point of view, VLAs are only supported in C, and only from C99 on. They are not supported in C++.

From a compiler point of view, g++ will support VLAs as extensions to C90 and C++, but if you compile with -pedantic it will disable those extensions and you'll get a compile error.

Visual Studio does not support VLAs at all in either C or C++. VS only supports up to the C89 standard, and AFAIK MS has no plans to support the later C standards at all.

As far as scope is concerned, that's defined in the C standard:

6.7.6.2 Array declarators
...
2 If an identifier is declared as having a variably modified type, it shall be an ordinary identifier (as defined in 6.2.3), have no linkage, and have either block scope or function prototype scope. If an identifier is declared to be an object with static or thread storage duration, it shall not have a variable length array type.

There are technical reasons why VLAs cannot be declared static or at file scope; objects with static storage duration are allocated at program startup and held until the program terminates, and if I'm not mistaken it's not guaranteed that objects will be allocated and initialized in any particular order. So those items need to have their size known at compile time.

这篇关于GNU编译器与数组上的Visual Studio分配w /范围内的长度常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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