Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Variable length arrays

Reply
Thread Tools

Variable length arrays

 
 
Kenny McCormack
Guest
Posts: n/a
 
      03-07-2008
In article <(E-Mail Removed)>,
Kaz Kylheku <(E-Mail Removed)> wrote:
>On Mar 6, 11:41*am, jacob navia <(E-Mail Removed)> wrote:
>> Nowhere in the standard it is written that I must free those
>> arrays
>>
>> The standard does not specify that those arrays should be destroyed.
>> They are no longer available, that's all.

>
>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.

 
Reply With Quote
 
 
 
 
SM Ryan
Guest
Posts: n/a
 
      03-07-2008
# 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
If your job was as meaningless as theirs, wouldn't you go crazy too?
 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      03-07-2008
On Mar 7, 1:17*am, jacob navia <(E-Mail Removed)> wrote:
> Kaz Kylheku wrote:
> > On Mar 5, 5:38 pm, Erwin Lindemann <(E-Mail Removed)> wrote:
> >> 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!
 
Reply With Quote
 
Erwin Lindemann
Guest
Posts: n/a
 
      03-08-2008
jacob navia wrote:

> 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
--
Erwin Lindemann

 
Reply With Quote
 
Erwin Lindemann
Guest
Posts: n/a
 
      03-08-2008
jacob navia wrote:

> 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!
>
> > Thanks

>
> You are welcome.


Thanks
--
Erwin Lindemann

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-08-2008
jacob navia <(E-Mail Removed)> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable-length arrays: should they be used at all? Rui Maciel C Programming 56 07-10-2012 06:12 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Initialization of variable length arrays jaime C Programming 3 06-14-2007 11:56 PM
Variable length arrays Q mechanicfem@googlemail.com C Programming 6 02-21-2006 04:32 AM
variable length arrays E. Robert Tisdale C Programming 21 10-13-2003 10:57 PM



Advertisments