Variable length arrays

  • Thread starter Erwin Lindemann
  • Start date
K

Kenny McCormack

Sure, the standard doesn't explicitly say anywhere that the generated
code must not introduce software defects which are not present in the
source code, such as memory leaks. So you're off the hook?

What Jacob is demonstrating (albeit subtly) is how the Clique members
act as if the standard is absolute truth (cannot be questioned and does
not need to be defended) - right up until it suits their purposes to do
otherwise.

Much as religious fundy leaders do, in re: the Bible.

As I've pointed out several times previously, CLC very much resembles
fundy religion - and the temperments of the folks here closely resembles
that of Bush voters.
Why not also generate code that randomly freezes? The standard doesn't
specify maximum time which must elapse from the evaluation of one
expression to the next.

Exactly so. Well done.
Boy, how quick are we to retreat to the standard. Did you know that it
doesn't even say there /is/ such thing as a stack? And here you are,
asking my compiler to clean up something that doesn't even exist!

Yes. The standard is absolutely true and valid, in all its particulars.
See above.
 
S

SM Ryan

# Regardless of whether block or procedure-level allocation is used,
# though, there must always be exactly one instance of the object.

This issue has been around since Algol 60. You can learn to
cope with the procedure level allocation or you can watch your
programs break.
 
K

Kaz Kylheku

Kaz said:
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*]
See, this is what happens when the compiler writer becomes infatuated
with alloca. :)

There are two kinds of people in this world.

Yes, indeed: those who dichotomize everything, those who think, and
those who cannot count.
The people that do not move a single finger, and limit themselves
to criticizing the first ones.

Like those goddamned software testers that keep making work for us!
 
E

Erwin Lindemann

jacob said:
Look, I work for the regulars here, fixing things that are not broken
like this same person that insisted on calling a function
"abs" and complaining that stdlib.h defines "abs" and that he
receives an error message


The same guy that starts now this thread.

What are you talking about? I never claimed such a thing. Furthermore,
according to ISO/IEC 9899:1999, 7.20.6.1, <stdlib.h> is *required* to
declare 'abs()' (well, declare, not define; sloppy wording on your part?)

Perhaps you are confusing me with somebody else?


Thanks
 
E

Erwin Lindemann

jacob said:
Erwin Lindemann wrote: [...]
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.

If I change the marked line above to

unsigned char vla[size*i+1];

and change the size expression in the memset call to
the same expression, gcc will produce the following
output in my linux box:
step 0: vla=0xbffff990
step 1: vla=0xbffbf990
step 2: vla=0xbff7f990
step 3: vla=0xbff3f990
step 4: vla=0xbfeff990
step 5: vla=0xbfebf990
step 6: vla=0xbfe7f990
step 7: vla=0xbfe3f990
step 8: vla=0xbfdff990
step 9: vla=0xbfdbf990

As you can see, the addresses are different each time.

If the size is different in each step, that is not surprising.
Have you tried printing the address of vla's last element, too?
gcc is doing simply a constant propagation. It notices that the
expression in the VLA size is a constant expression within the
affected block, and optimizes the case you presented above
to reuse always the same block.

I'm not convinced this is what happens. I think gcc is reusing
the space reserved for 'vla' each time its scope is left and
re-entered, in the same way it does so for fixed size arrays
and scalar object types.
Fine, maybe I could do such an optimization too, so that the
clique of comp.lang.c (where you belong)

I do? Usenet is a strange place.
can have a harder time, but what for? This optimization
would only optimize a special case.

It would also allow programs to work correctly that define
variable length array inside a loop body.
I will do it some day when I introduce strength reduction into
loop bodies. But I am skeptical of those classic optimizations.
I am at 80-90% of the speed of gcc in many benchmarks, but the
compilation speed of lcc-win is 600-800% better than gcc. I
prefer this situation.

Which situation? The optimization you are talking about or the
memory leakage I discovered?
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?

Should a bug report be filed?

You can only file a bug report if you buy maintenance at premium rates.

Well, you already know about the problem now, and expressed you are not
going to do anything about it. How would buying "maintenance at premium
rates" help in this situation?
I have a special price for clique members sorry!


You are welcome.

Thanks
 
K

Keith Thompson

jacob navia said:
Allocating a variable length array within a loop?
Look, I work for the regulars here, fixing things that are not broken
like this same person that insisted on calling a function
"abs" and complaining that stdlib.h defines "abs" and that he
receives an error message
[...]

It wasn't "abs" (which must be defined in <stdlib.h>). lcc-win's
stdlib.h defined "min" and "max" macros in <stdlib.h>, in violation of
the standard. See the "min/max in stdlib.h?!" thread from January,
starting with
<http://groups.google.com/group/comp.lang.c/msg/26e05e43e19aac05>.

You responded at the time that they would go away in conforming mode
in the next release.

I congratulate you on your speed in correcting errors that are
reported here. But since I personally care more about this newsgroup
than about your compiler (nothing personal, I'm just not a Windows
programmer), I'd be happier without the drama that ensues between the
bug report and the fix.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top