|
Is this a deliberate gcc bug ??
Author |
Message |
rohi #1 / 4
|
Is this a deliberate gcc bug ??
hi, iam using the gcc compiler gcc-2.95.i think i have found a bug in gcc. the following code gives me output 0,1 code: int main() { int a[0]={1}; printf(" sizeof a == %d , a[0] == %d \n",sizeof(a),a[0]); Quote: }
but if i write int a[1] = {1,2}; and print it out it gives an error "excess element in array initializer ".but it dosent give any error in the above case. i can understand the second case that its incorrect to initialize the array like this, but the same goes for the first one also. is there any special case case for zero or is this a bug of gcc. thanks rohit
|
Sun, 03 Apr 2005 22:31:03 GMT |
|
|
Dan P #2 / 4
|
Is this a deliberate gcc bug ??
Deliberate bugs are called features :-) Quote: >iam using the gcc compiler gcc-2.95.i think i have found a bug in gcc. >the following code gives me output >0,1 >code: >int main() >{ > int a[0]={1}; > printf(" sizeof a == %d , a[0] == %d \n",sizeof(a),a[0]); >} >but if i write > int a[1] = {1,2}; >and print it out it gives an error "excess element in array >initializer ".but it dosent give any error in the above case. >i can understand the second case that its incorrect to initialize the >array like this, but the same goes for the first one also.
And gcc correctly diagnoses it, if invoked as a C compiler: fangorn:~/tmp 159> gcc -ansi -pedantic test.c test.c: In function `main': test.c:3: warning: ANSI C forbids zero-size array `a' Which basically tells you that your code is broken, because you can't define zero sized arrays in C. Quote: >is there any special case case for zero or is this a bug of gcc.
By default, gcc is not a C compiler, it is a GNU C compiler and zero sized arrays are a GNU C feature (I completely ignore the semantics of initialising such an array). Dan -- Dan Pop DESY Zeuthen, RZ group
|
Sun, 03 Apr 2005 23:06:02 GMT |
|
|
EPers #3 / 4
|
Is this a deliberate gcc bug ??
Quote:
> hi, > iam using the gcc compiler gcc-2.95.i think i have found a bug in gcc.
You have? Why don't you follow the bug reporting directions in the gcc documentation? (OT: the latest release of gcc is in the 3.2's.) Quote: > the following code gives me output > 0,1 > code: > int main() > { > int a[0]={1};
There is no such thing as an array of length 0. A diagnostic is required (even without the initializer). [snip] Eric Schmidt
|
Mon, 04 Apr 2005 05:11:01 GMT |
|
|
Martin Ambuh #4 / 4
|
Is this a deliberate gcc bug ??
Quote:
> hi, > iam using the gcc compiler gcc-2.95.i think i have found a bug in gcc. > the following code gives me output > 0,1 > code: > int main() > { > int a[0]={1}; > printf(" sizeof a == %d , a[0] == %d \n",sizeof(a),a[0]); > } > but if i write > int a[1] = {1,2}; > and print it out it gives an error "excess element in array > initializer ".but it dosent give any error in the above case. > i can understand the second case that its incorrect to initialize the > array like this, but the same goes for the first one also. > is there any special case case for zero or is this a bug of gcc.
/* You must not have your diagnostic levels set appropriately. For the following code: */ #include <stdio.h> /* include this if you want to use printf */ void firstcase() { /* claimed to output "0,1" */ int a[0] = { 1 }; printf(" sizeof a == %d , a[0] == %d \n", sizeof(a), a[0]); Quote: }
void secondcase() { /* claimed to emit error message */ int a[1] = { 1, 2 }; printf(" sizeof a == %d , a[0] == %d \n", sizeof(a), a[0]); Quote: }
int main(void) { firstcase(); secondcase(); return 0; Quote: }
/* GCC gives me these diagnostics: a.c: In function `firstcase': a.c:7: warning: ISO C forbids zero-size array `a' a.c:7: warning: excess elements in array initializer a.c:7: warning: (near initialization for `a') a.c:8: warning: int format, long unsigned int arg (arg 2) a.c: In function `secondcase': a.c:13: warning: excess elements in array initializer a.c:13: warning: (near initialization for `a') a.c:14: warning: int format, long unsigned int arg (arg 2) */
|
Mon, 04 Apr 2005 16:54:04 GMT |
|
|
|