On Thu, 6 Mar 2008 02:38:17 +0100 (CET), Erwin Lindemann
<> wrote in comp.lang.c:
>
> If a VLA appears within a loop body, it seems the behavior is
> different with two different compilers I tried. I looked at the
> standard text, but couldn't find a definite answer there either.
There is no definitive answer. All the standard says about VLAs that
might be relevant is this:
"For such an object that does have a variable length array type, its
lifetime extends from the declaration of the object until execution of
the program leaves the scope of the declaration. If the scope is
entered recursively, a new instance of the object is created each
time. The initial value of the object is indeterminate."
It says nothing at all about whether each creation must, may, or may
not have the same address. So it is entirely a QOI issue.
> Consider the following test program
>
> /* begin foo.c */
> #include <stdio.h>
> #include <string.h>
>
> void test(int n, size_t size)
> {
> int i;
>
> for(i = 0; i < n; i++) {
> unsigned char vla[size];
> memset(vla, (i & 255), size);
> printf("step %d: vla=%p\n", i, &vla[0]);
> }
> }
>
> int main(void)
> {
> test(10, 256*1024L);
> return 0;
> }
> /* end foo.c */
>
> With gcc, 'vla' is reused in every iteration, i.e., the address
> of 'vla[0]' is identical in every step.
>
> However, with lcc-win32, output is as follows...
>
> step 0: vla=0x002ffea0
> step 1: vla=0x002bfea0
> step 2: vla=0x0027fea0
> step 3: vla=0x0023fea0
> [*CRASH*]
>
> , meaning, new storage is allocated for 'vla' at every iteration,
> eventually exhausting all available auto storage.
>
> Now, is this just implementation dependant and this kind of construct
> should be avoided, or is one of these compilers not working correctly?
Since the standard does not require either behavior, it is not a
conformance defect that I can see. It is a QOI issue, and I suggest
you contact the offending compiler's implementer, either directly or
on his support group.
> Should a bug report be filed?
Since the standard does not guarantee that the creation of any array
of automatic duration will succeed, even if it is not a VLA, it's hard
to see a complaint on standard conformance grounds, but it's a huge
QOI issue.
I'd complain if I used this feature of his compiler, although I have
plonked him and don't use his compiler at all anymore.
Consider a similar case:
#include <stdio.h>
void func(int x)
{
printf("%p\n", (void *)&x;
}
int main(void)
{
int x = 0;
func();
func();
func();
return x;
}
I don't think I have ever used an implementation where the three calls
to func() would output different values.
Do you think the C standard requires it to be the same? If not, do
you think it should?
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html