编译器如何处理变长数组

How do compilers treat variable length arrays(编译器如何处理变长数组)
本文介绍了编译器如何处理变长数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这似乎是一个初学者的问题,但我对编译器通常创建可变维度数组的方式感兴趣,如下面的程序所示.

This might seem like a beginner's question, but I am interested in the way that a compiler normally creates arrays of variable-dimensions, like in the following program.

#include<iostream>

int main(){
  int n;
  std::cin>>n;
  int a[n];
}

据我所知,在 C 中,所有初始值设定项的值都必须是常量,以便编译器知道要在函数内部保留多少内存,通常通过减去堆栈指针以容纳数组元素的数量持有.

From what I've learnt, in C all the initializer values must be constant, so that the compiler knows how much memory to reserve inside the function, normally by subtracting the stack pointer in order to accomodate the number of elements the array holds.

这对我来说很有意义.但是,我不太明白编译器如何处理上述程序,因为它似乎可以与 G++(MinGW) 一起使用,但与 Microsoft 的 C++ 编译器 Cl 一起失败.我怀疑 GCC 通过非标准扩展在堆上分配内存,但我不确定这一点.

This makes sense to me. However, I don't quite understand how compilers treat the above program, since it seems to work with G++(MinGW) , but fails with Cl, Microsoft's C++ compiler. I suspect that GCC allocates memory on the heap trough a non-standard extension, but I am not sure of this.

此外,Microsoft 的编译器并不以符合标准而著称,因此如果它处理上述程序的方式实际上可能是错误的,我也不会感到惊讶.

Also, Microsoft's compiler is not renowned for being standards-compliant, so I wouldn't be surprised if it may actually be wrong in the way it treats the above program.

推荐答案

在 C 标准的 C99 版本中,允许使用可变长度数组.但是,在任何版本的 C++ 中都不允许使用它们;你看到的是 G++ 扩展.请注意,微软的 C 编译器并不完全支持 C99;由于 G++ 支持 C99,因此很容易将 VLA 支持应用于 C++ 作为扩展.

In the C99 version of the C standard, variable length arrays are permitted. However, they are not permitted in any version of C++; you're seeing a G++ extension. Note that Microsoft's C compiler does not fully support C99; since G++ supports C99 it's easy enough to apply the VLA support to C++ as an extension.

至于编译器通常如何实现 VLA,它与 alloca() 相同(除了它必须保持 sizeof 的大小) - 编译器保存原始堆栈指针,然后根据它计算出的所需字节数向下调整它.缺点是函数入口和出口有点复杂,因为编译器需要存储将堆栈指针重置到的位置,而不仅仅是通过固定常量进行调整.

As to how the compiler usually implements VLAs, it's the same as alloca() (except that it has to keep the size around for sizeof) - the compiler saves the original stack pointer, then adjusts it down by however many bytes it calculates that it needs. The downside is function entry and exit is a bit more complicated as the compiler needs to store where to reset the stack pointer to rather than just adjusting by fixed constants.

这篇关于编译器如何处理变长数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Deduce template argument from std::function call signature(从 std::function 调用签名推导出模板参数)
check if member exists using enable_if(使用 enable_if 检查成员是否存在)
Standard Library Containers with additional optional template parameters?(具有附加可选模板参数的标准库容器?)
Uses of a C++ Arithmetic Promotion Header(C++ 算术提升标头的使用)
Parameter pack must be at the end of the parameter list... When and why?(参数包必须位于参数列表的末尾...何时以及为什么?)
quot;not declared in this scopequot; error with templates and inheritance(“未在此范围内声明模板和继承错误)