tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] Best workaround for VLA's


From: Joe Soroka
Subject: Re: [Tinycc-devel] Best workaround for VLA's
Date: Thu, 7 Apr 2011 23:32:47 -0700

On Thu, Apr 7, 2011 at 9:50 PM, Luis Alejandro Muzzachiodi
<address@hidden> wrote:
>
> Until now i was using successfully TCC with a multiplatform library.
> However in the last time the author begin to use VLA's (and tough i'm trying 
> that not, it's very possible that he keeps and/or increases more your use).
> So, what's would be the best workaround in TCC for stuff like:
>      char str[i+15];
> or
>      float flt[veclen];
> etc.?
> I mean, the simplest option could be the max possible length, sure; but i 
> haven't idea of max size of these arrays ...
> Having this on mind, what's the best alternative (if exists...) ?.

I'm planning to push a modified version of Thomas Preud'homme's VLA
patch very soon that might solve your problem.

But basically what I've been doing, is just converting those things to
alloca() calls.

So,
    char str[i+15];
    float flt[veclen];
becomes
    char *str = alloca(sizeof(char)*(i+15));
    float *flt = alloca(sizeof(float)*veclen);

The problem with that approach is that it doesn't work with
multidimensional arrays, doesn't work with function parameters, and
most importantly doesn't work with sizeof.

For example, there's no alloca equivalent for multidimensional dereferencing:
    char str[i+15][10];
    str[0][0] = 123;

Also, there's no obvious equivalent for something like the VLA example
in "C in a nutshell":
    double maximum(int nrows, int ncols, double matrix[nrows][ncols])
{ /* ... */ }

Finally,
    float flt[veclen];
    memset(flt, 0, sizeof(flt));
doesn't work.
You can of course just use
    memset(flt, 0, sizeof(float)*veclen);
instead, but "veclen" may have changed since the array was allocated,
say if it was reused for some other purpose in between.

Also you probably should '#include <alloca.h>' which can be slightly annoying.

..



reply via email to

[Prev in Thread] Current Thread [Next in Thread]