Re: VLA and goto -- diagnostic required?
- From: "Robert Gamble" <rgamble99@xxxxxxxxx>
- Date: 24 Aug 2006 16:45:47 -0700
Man with Oscilloscope wrote:
goto jumping over vla -- diagnostic required?
This is a question about C99, 6.8.6.1, example 2 (see test below).
I'm currently working on updating an older compiler up to C99. The
standard is very clear about jumping into and out of the middle of
a block declaring variably modified types. The example below,
however, just jumps over the actual VLA declaration, within the
/same/ block. A quick test with two different compilers claiming
(at least partial) C99 conformance reveals...
The lcc-win32 compiler (tested with version 3.8) accepts it without
any diagnostics, but generates unusable code (more specifically,
the `goto' jumps past the instructions necessary to set up space
for `vla[]').
Gcc (all version with (partial) C99 support), on the other hand,
reject the example with a hard error.
What is the best course of action here? Is this an example of
"everone who writes such code deserved what they get", or is a
strictly conforming compiler required to reject it?
--8<---------------------------------------------------------------
/*
* vlatest.c
*
* gcc -Wall -W -O2 -std=c99 vlatest.c -o vlatest
* ("vlatest.c:26: error: label `bar' used before \
* containing binding contour")
*
* lc -A -O -ansi -unused vlatest.c
* (no diagnostic printed, invalid executable generated, BOOM!)
*/
#include <stdio.h>
#include <string.h>
int foo = 1;
void vlatest(size_t size)
{
printf("vlatest...\n");
if(foo)
goto bar;
int vla[size];
bar:
memset(vla, 0xCC, sizeof vla);
printf("BAMM!!!\n");
}
int main(void)
{
vlatest(1024);
return 0;
}
Here is the relevant constraint from the Standard:
"A goto statement shall not jump from outside the scope of an
identifier having a variably modified type to inside the scope of that
identifier."
Note carefully the wording: "from outside the scope ... to inside the
scope". The scope of vla begins immediately after its declaration and
ends at the end of the function vlatest. Your example jumps from
outside the scope of vla to inside the scope of vla. That is a
constraint violation requiring the issue of a diagnostic.
Robert Gamble
.
- References:
- VLA and goto -- diagnostic required?
- From: Man with Oscilloscope
- VLA and goto -- diagnostic required?
- Prev by Date: Re: Bitshifting independant of signedness
- Next by Date: Re: A newbie's code
- Previous by thread: Re: VLA and goto -- diagnostic required?
- Next by thread: Re: VLA and goto -- diagnostic required?
- Index(es):
Relevant Pages
|