Gcc 4.8.2默认编译并运行可变长度数组
在C编程中,我遇到了这样一种情况:我不小心初始化了一个大小可变的数组,结果它成功了。我做了一些研究,显然C99编译中提供了可变长度数组。显然,GCC4.8.2的默认编译选项是C98 以下是我用来测试的代码:Gcc 4.8.2默认编译并运行可变长度数组,c,gcc,variable-length-array,C,Gcc,Variable Length Array,在C编程中,我遇到了这样一种情况:我不小心初始化了一个大小可变的数组,结果它成功了。我做了一些研究,显然C99编译中提供了可变长度数组。显然,GCC4.8.2的默认编译选项是C98 以下是我用来测试的代码: #include "stdio.h" #include "stdlib.h" // rand(), srand() #include "time.h" void printArray(const char* c) { // impossible to get size of c b
#include "stdio.h"
#include "stdlib.h" // rand(), srand()
#include "time.h"
void printArray(const char* c) {
// impossible to get size of c because it returns pointer size
int array[sizeof(c)/sizeof(char)];
int i;
for(i=0; i<(sizeof(c)/sizeof(char))-1; i++) {
int fill=-1;
if(c[i]=='a')
fill = 0;
else if(c[i]=='b')
fill = 1;
array[i]=fill;
}
printf("contents of array in binary: \n");
for(i=0; i<(sizeof(c)/sizeof(char))-1; i++) {
printf("%d, ", array[i]);
}
printf("\n");
}
void printRandomArray() {
srand(time(NULL));
// variable length array is possible using C99
int array[rand()%10];
int i;
printf("contents of random array: \n");
for(i=0; i<(sizeof(array)/sizeof(int)); i++) {
array[i]=rand()%10;
printf("%d, ", array[i]);
}
printf("\n");
}
int main(int argc, char* argv[]) {
char c[]="abbabbabbaababababababb";
printArray(c);
printRandomArray();
return 1;
}
#包括“stdio.h”
#包括“stdlib.h”//rand()、srand()
#包括“time.h”
void打印数组(常量字符*c){
//无法获取c的大小,因为它返回指针大小
int数组[sizeof(c)/sizeof(char)];
int i;
对于(i=0;i除了C99标准之外,GCC还允许可变长度数组作为扩展:
.
<> P>>在ISO C99中允许可变长度自动数组,作为扩展GCC接受C90模式和C++中的.
AFAIK,GCC 4.8.2默认以-std=gnu90
模式编译代码。使用选项-std=c89
编译代码,您将看到大量警告和错误。因此GCC支持两个不同版本的c89。它支持c89
和gnu89
。后者意味着启用了许多GCC扩展
GCC 4.8.2的默认标准语言为gnu90
,与gnu89
相同
让我们看一下使用这些不同语言时出现的不同警告/错误:
GNU89和GNU90
C89
首先,没有C98,它是C89/90,然后是C95(通常合并到C89/90中),然后是C99等等
其次,默认模式下的GCC编译器根本不实现任何标准C。它正在编译的语言称为GNU C。默认情况下,它支持VLA没有什么不寻常的地方。您必须手动配置GCC,以使其符合任何C标准。像-pedantic
或-pedantic errors
之类的开关是必须。Bill Lynch您的答案正是我想要的,因为它解释了为什么使用不同的C规范编译的东西碰巧在GCC 4.8.2默认情况下得到支持,为什么您要删除?@BillLynch try-std=c89-pedantic
和-Werror
或-Werror=vla
@BLUEPIXY:啊。我们看到了开始。谢谢!@user3064869:BLUEPIXY的想法是添加-pedantic
,现在它已经备份了。有趣的是:GCC(4.9.1测试)接受-std=c89
和-std=c90
这两个选项,它的意思基本相同;同上-std=gnu89
和-std=gnu90
。我的意思是,89和90的选项都被视为等效和有效的。我正在处理的代码中有//code>注释,而不是上面的VLA代码,我得到了半解析-适当的警告(因为代码非常模糊,测试#准确地说包括)。明确地说,我之前根本没有测试问题中的代码。当我测试问题中的代码时,/
注释被拒绝(警告)使用-std=c89
。然而,直到我使用-std=c89-pedantic
(或-std=gnu89-pedantic
)时,VLA才被拒绝。即使如此,在我添加了-Werror
@JonathanLeffler之前,这只是一个警告;我仍然感到惊讶。如果您在GCC 4.8.2中使用选项-std=c89
编译相同的代码,您将得到如此多的警告和错误。我在c89非学究模式下编译的问题中得到了1个警告和4个错误,主要是因为e> //
注释。使用/**/
注释替换注释后,代码将在不使用-pedantic
的情况下干净地编译,并且在使用-pedantic
编译时带有一条关于使用VLA的警告。
[2:10pm][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/gcc asd.c
[2:10pm][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/gcc -std=gnu89 asd.c
[2:10pm][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/gcc -std=gnu90 asd.c
[2:10pm][wlynch@apple /tmp]
[2:10pm][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/gcc -std=c89 asd.c
asd.c:2:21: warning: extra tokens at end of #include directive [enabled by default]
#include "stdlib.h" // rand(), srand()
^
asd.c: In function ‘printArray’:
asd.c:6:5: error: expected expression before ‘/’ token
// impossible to get size of c because it returns pointer size
^
asd.c:15:9: error: ‘array’ undeclared (first use in this function)
array[i]=fill;
^
asd.c:15:9: note: each undeclared identifier is reported only once for each function it appears in
asd.c: In function ‘printRandomArray’:
asd.c:26:5: error: expected expression before ‘/’ token
// variable length array is possible using C99
^
asd.c:30:24: error: ‘array’ undeclared (first use in this function)
for(i=0; i<(sizeof(array)/sizeof(int)); i++) {
^
[2:10pm][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/gcc -std=gnu89 asd.c
[2:10pm][wlynch@apple /tmp]
[2:28pm][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/gcc -std=c89 -pedantic asd.c
asd.c: In function ‘printRandomArray’:
asd.c:27:5: warning: ISO C90 forbids variable length array ‘array’ [-Wvla]
int array[rand()%10];
^
asd.c:27:5: warning: ISO C90 forbids mixed declarations and code [-Wpedantic]