Article View: comp.lang.c.moderated
Article #2588Re: Legal alloca?
From: Lars Wirzenius
Date: Sat, 13 Apr 1996 14:36
Date: Sat, 13 Apr 1996 14:36
51 lines
1791 bytes
1791 bytes
dtrg@st-andrews.ac.uk (David Thomas Richard Given):
> I think I mentioned using alloca() on this newsgroup before, and received
> quite a lot of mail telling me that it was nonstandard, nonportable, and
> that use of it was generally heinous. :)
>
> The problem is, it's so useful. There's a lot of cases when I write
> programs when I allocate a variable-sized buffer at the beginning of a
> function. If I use malloc(), I have to remember to free it at the end.
Instead of this:
int foo(int bar) {
int *temp;
temp = alloca(bar*sizeof(int));
if (temp == NULL)
return -1;
... do funny things with temp and bar and whatever ...
return 0;
}
Do this:
static int aux_foo(int bar, int *temp) {
... do funny things with temp and bar and whatever ...
}
int foo(int bar) {
int *temp;
int ret;
temp = malloc(bar*sizeof(int));
if (temp == NULL)
return -1;
ret = aux_foo(bar, temp);
free(temp);
return ret;
}
> My question is, is there a legal alloca() I can use anywhere?
I don't think so. See the FAQ, question 7.32.
> I'd really like to be able to use this function. Can I?
How portable do you wish to be?
--
Lars Wirzenius <liw@iki.fi> <wirzeniu@kruuna.helsinki.fi>
MIME and PGP mail welcome. Key: finger @kruuna, or check keyservers.
Publib http://www.iki.fi/liw/publib/
Technical Details
Message-ID:
<4koe4u$8sl@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4klidc$ne8@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: wirzeniu@cc.helsinki.fi (Lars Wirzenius) Newsgroups: comp.lang.c.moderated Subject: Re: Legal alloca? Date: 13 Apr 1996 09:36:14 -0500 Organization: University of Helsinki Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4koe4u$8sl@solutions.solon.com> References: <4klidc$ne8@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com