我应该信任哪个编译器? [英] Which compiler should I trust?

查看:46
本文介绍了我应该信任哪个编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是一个新手问题,但我试图用 C 语言(不是 C++)做一个小练习,我正在运行成一些问题.

This is going to be some what of a newbie question but I was trying to work on a small exercise in the C Language (not C++) and I was running into some issues.

假设我想在一个方法中使用一个数组,其大小取决于参数之一:

Say I wanted to use an array within a method whose size depended on one of the arguments:

void someFunc(int arSize)
{
    char charArray[arSize];
    // DO STUFF
    ...
}

当我尝试在 Visual Studio 2013 中将其编译为 .c 文件时,我收到一条错误消息,指出不允许使用非常量数组大小.然而,相同的代码在 GNU 编译器下的 CodeBlocks 中工作.我应该相信哪个?编译器的行为如此不同是正常的吗?我一直认为,如果你正在做一些编译器不喜欢的事情,你一开始就不应该做,因为它不是标准.

When I try to compile this as a .c file within Visual Studio 2013 I get an error saying that a non-constant array size is not allowed. However the same code works within CodeBlocks under a GNU Compiler. Which should I trust? Is this normal for compilers to behave so differently? I always thought that if you're doing something that a compiler doesn't like you shouldn't be doing it in the first place because it's not a standard.

任何输入都是有用的!我来自 Python 背景,我正在努力更多地参与数据结构和算法编程.

Any input is useful! I come from a Background in Python and I am trying to get more heavily involved in programming with Data-Structures and Algorithms.

您可能知道,我的平台是 Windows.如果这个问题需要更多信息才能回答,请告诉我.

My platform is Windows as you can probably tell. Please let me know if this question needs more information before it can be answered.

推荐答案

可变长度数组 (VLA) 是 C99 功能和 Visual Studio 直到最近确实不支持 C99,我不确定它目前是否支持最新版本的 VLA.另一方面,gcc 确实支持 C99,尽管 不完全.gcc 支持 VLA 作为 C99 模式之外的扩展,即使在 C++ 中.

Variable length arrays(VLA) are a C99 feature and Visual Studio until recently did not support C99 and I am not sure if it currently supports VLA in the lastest version. gcc on the other hand does support C99 although not fully. gcc supports VLA as an extension outside of C99 mode, even in C++.

来自 C99 标准草案section 6.7.5.2 Array declarators 第 4 段:

From the draft C99 standard section 6.7.5.2 Array declarators paragraph 4:

[...] 如果大小为整型常量表达式且元素类型具有已知常量大小,则该数组类型不是变长数组类型;否则,数组类型为变长数组类型.

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

这篇关于我应该信任哪个编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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