Re: Criticisms?



Richard Heathfield wrote:
"No semi-dynamic (i.e. stacked, runtime-sized) arrays until the
C99 standard (despite not requiring garbage collection)" - it's a
shame that C99 has introduced this pointless feature. C already
has the facility to create arrays at runtime *with failure detection*,
and so a facility that lacks failure detection seems rather silly.

Actually, VLAs give you more than just that one particular feature.
In particular, you can now use VLAs to handle multidimensional
array parameters conveniently:

void mat_whatever(size_t nrows, size_t ncols, double mat[nrows][ncols]) {
... operation on the matrix mat ...
}

Although this is usually do-able in C89, you have to at least
technically violate array bounds if you work on an actual matrix
(i.e., pass &mat[0][0] in the caller, and do and your own "manual"
indexing, accessing p[r * ncols + c], to get at mat[r][c]).

In article <51ba87F1jhj65U13@xxxxxxxxxxxxxxxxxx>
Ian Collins <ian-news@xxxxxxxxxxx> wrote:
Do you think the common Unix function alloca(), which allocates memory
from the stack should have been standardised?

It can be a direct replacement for VLAs with failure detection and I've
often wondered why it wasn't included in the standard library.

For alloca() to be reliable, the compiler has to recognize calls
to it. Try something like:

foo(bar(),
128, alloca(128),
baz(i, alloca(i), j),
32, alloca(32),
zog(42));

on a straightforward Intel x86 compiler that does not recognize
alloca(), for instance, and then observe the carnage. :-) (The
problem is that each alloca() call adjusts the stack while the
compiler is building the stack that is going to be used to call
the functions involved. The stack thus built interleaves "alloca
created stack space" with "compiler created stack space", and
everything goes haywire when pieces of this are removed. Note
also that the behavior tends to change with optimization levels,
when post-function-call stack adjustments may be deferred.)

Given that alloca() must, in order to be reliable, be a compiler
built-in, I believe it makes more sense to just have VLAs as a
compiler built-in.

Note that the duration of VLAs is well-controlled and interacts
"correctly" with other block-scope variables:

for (int i = k; i < max; i += step) {
double a, b, tmp[i];
...
if (finished)
break;
...
}

but this is not true for alloca() space:

for (int i = k; i < max; i++) {
double a, b, *tmp;

tmp = alloca(i * sizeof *tmp);
...
if (finished)
break;
...
/* tmp is too small, we should get rid of it here */
/* unalloca(tmp); -- there is no way to do this */
}

While it is possible to come up with cases where "function duration"
objects -- what alloca() produces, if it works at all -- are useful,
this seems to be considerably rarer than normal automatic-duration
(block scope automatic) variables.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
.



Relevant Pages

  • Re: What is a stack frame?
    ... There is no need for a separate "this frame" pointer. ... In any case, the more general term, which covers what a C compiler ... hardware-provided stack to implement the stack-like data structure ... manner since the current stack frame is "deformed" by alloca. ...
    (comp.lang.c)
  • Re: alloca
    ... a compiler can decide to _always_ perform the calls to ... That's just another way to circumvent the potential problem with using 'alloca' in an argument list. ... A quality implementation might perform a run-time stack integrity check in debug configuration and trigger a run-time diagnostic. ... Although I don't direct connection to the 'alloca' being standard or not. ...
    (comp.lang.c)
  • Re: FindFirstFile Possible Memory Leak
    ... for compiler variables that stack data is only ... function called alloca. ... This was suppose to allocate a local buffer on the stack and return its ... architectures, the stack pointer can be changed without losing your ...
    (microsoft.public.vb.general.discussion)
  • Re: FindFirstFile Possible Memory Leak
    ... Loop optimization and deeper parsers for ... for compiler variables that stack data is only ... stack pointer. ... function called alloca. ...
    (microsoft.public.vb.general.discussion)
  • Re: [C99] local VLA with dimension given by a global var?
    ... (it will run out of stack in the recursion). ... why should VLAs be any different? ... If you try to force compilers (in the standard) to put in code to catch ... Except that there are systems on which alloca() is difficult to ...
    (comp.lang.c)