alloca() portability
Fred Bourgeois
fjb at metaware.metaware.com
Sat Nov 17 05:12:52 AEST 1990
In article <27634 at mimsy.umd.edu> chris at mimsy.umd.edu (Chris Torek) writes:
>In article <PAULB.90Nov12131357 at hcx2.ssd.csd.harris.com>
>paulb at ssd.csd.harris.com (Paul Beusterien) writes:
>>>In my opinion alloca is the cleanest way to allocate memory that is needed
>>>for the lifetime of a single routine. ... [comparison with malloc deleted]
>I disagree; however, we may well be on the same side:
Clean or not clean, it is the fastest way to allocate memory needed only for
the lifetime of the current routine. Assuming your compiler implements it
properly. If you're not sure, use the `#define alloca malloc' method.
>There may be major correctness losses using alloca (as I wrote earlier,
>any optimizing compiler can easily produce failing object code from
>apparently-correct source code if that source code uses alloca).
>(For fun, try compiling
[example deleted]
>on fifty different architectures or so, and count success to failure
>ratios. You may have to compile the first without any optimisation.)
Oh come on! You can't claim this as a problem with alloca! The standard
clearly states (Appendix F.1) that "the order in which side-effects take
place" is unspecified. And clearly, alloca has a side effect - it modifies
the (possibly virtual) stack pointer. Also consider 3.3.2.2, the order of
evaluation of arguments to a function is unspecified. Try the following on
fifty different architectures and see what results you get:
#include <stdio.h>
/* insert your favorite push/pop functions */
extern int push(int s); /* push returns argument pushed */
extern int pop(void);
int main() {
#if defined OPT
printf("stack(%d %d)\n", push(23), pop());
#else
int a = push(23);
printf("stack(%d %d)\n", a, pop());
#endif
}
This is just another area in which the programmer must be aware of a subtlety
in the language.
>In article <s64421.658490039 at zeus> s64421 at zeus.usq.EDU.AU (house ron) writes:
>>ABsolutely! It is a crying shame that alloca is not in ANSI. Of
>>course the real culprit is the absence of declarations with variable
>>bounds (int a[n]).
>
>Exactly. Alloca is not the solution. Alloca is a dirty hack. The
>solution to the problem at hand---namely, allocating local space, as
>opposed to heap space, when the amount of space is not known at compile
>time---should be something directly in the language.%
[stuff deleted]
I agree, the standard should specify a method for allocating local space.
Given good compiler support for alloca, and programmer awareness of the
pitfalls (e.g. side-effects are not portable, etc.), alloca is an adequate
solution *for the time being*. In my opinion, it should eventually be made
a part of the standard, because it is also the fastest way to allocate local
memory.
---
Fred Bourgeois, MetaWare Inc., 2161 Delaware Avenue, Santa Cruz, CA 95060-2806
fjb at metaware.com ...!uunet!metaware!fjb
Colorless Green Ideas Sleep Furiously, and so do I.
More information about the Comp.lang.c
mailing list