Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #66497

Re: lalloc() -- local stack allocation

Path csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: lalloc() -- local stack allocation
Date Tue, 28 Jul 2015 08:35:45 -0700
Organization None to speak of
Lines 69
Message-ID <lnd1zcnvla.fsf@kst-u.example.com> (permalink)
References <e9abe69a-2532-44f3-be7b-aaa236783cfb@googlegroups.com> <oJednSrtZtp2ZCvInZ2dnUU7-KWdnZ2d@posted.internetamerica>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="16907"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX186AGpY9k92OaRu8NpYaPUI"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Cancel-Lock sha1:fLr3+r3M81sxsO/SBA5sJn83O0Y= sha1:A5DgbzYpRkVjt1mFJWCxCHBN454=
Xref csiph.com comp.lang.c:66497

Show key headers only | View raw


gordonb.zziiu@burditt.org (Gordon Burditt) writes:
> "Rick C. Hodgin" <rick.c.hodgin@gmail.com> writes:
>> Does C define a type of lalloc() function for local / stack allocation?
>
> No.
>
> It sounds like you are talking about the GNU alloca() function.
> I encountered it long before C11's optional run-time-dimensioned
> arrays.

(I re-added an attribution line.)

alloca() is not specific to GNU.

Variable-length arrays were introduced in C99.  C11 made them optional.

[...]

> It's supposed to work to allocate memory inside a loop, so you have
> to keep track of how much memory was allocated, or save and restore
> the stack pointer at function entry and exit.

Does "you" here refer to the compiler? The programmer doesn't have to
keep track of any of this (other than to avoid allocating too much
memory).

> It is pretty much impossible to implement an alloca() nontrivially
> that works without compiler special treatment of the function.  (If
> you are writing your own compiler, that's not a problem.  If you
> are trying to retrofit it to an existing compiler, even if you have
> source code, it may not be so easy.) A naive compiler will badly
> mis-generate code for a function call where the arguments each call
> alloca() and the number of arguments well exceeds the square of the
> number of (temporary) registers used in passing arguments.  The
> stack adjustments for alloca() and the stack adjustments for pushing
> arguments on the stack for the call are going to get intermixed.

The Linux alloca(3) man page says:

    On many systems alloca() cannot be used inside the list of
    arguments of a function call, because the  stack  space
    reserved  by alloca() would appear on the stack in the middle
    of the space for the function arguments.

(It also says that there's no check for stack overflow; again, the
standard would describe this as undefined behavior.)

> What is worse, when I presented such a problem, the pro-alloca
> camp just said "Well, don't do that", but they couldn't describe the
> circumstances I would have to avoid.

A function argument should not include a call to alloca().

>                                       A compiler which allocated
> and de-allocated variables local to an inner block of a function
> at block entry and exit was a potential problem.

I've never heard of an implementation that would have problems calling
alloca() in an inner block, and there's no mention of any such problem
in the Linux man page.  Do you know of a specific example?

As always, I do not grant permission to quote this article without a
proper attribution line.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

lalloc() -- local stack allocation "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-12 02:23 -0700
  Re: lalloc() -- local stack allocation Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-07-12 11:48 +0200
    Re: lalloc() -- local stack allocation Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-07-12 11:48 +0200
  Re: lalloc() -- local stack allocation Philipp Klaus Krause <pkk@spth.de> - 2015-07-12 13:33 +0200
  Re: lalloc() -- local stack allocation Robert Wessel <robertwessel2@yahoo.com> - 2015-07-12 06:54 -0500
  Re: lalloc() -- local stack allocation Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-12 06:17 -0700
    Re: lalloc() -- local stack allocation Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-07-12 16:47 +0200
      Re: lalloc() -- local stack allocation Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-12 11:12 -0700
      Re: lalloc() -- local stack allocation supercat@casperkitty.com - 2015-09-08 11:53 -0700
        Re: lalloc() -- local stack allocation "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-08 12:16 -0700
          Re: lalloc() -- local stack allocation supercat@casperkitty.com - 2015-09-08 15:46 -0700
            Re: lalloc() -- local stack allocation "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-08 16:12 -0700
        Re: lalloc() -- local stack allocation Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 20:32 +0100
        Re: lalloc() -- local stack allocation jadill33@gmail.com - 2015-09-08 13:25 -0700
  Re: lalloc() -- local stack allocation "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-12 10:17 -0700
  Re: lalloc() -- local stack allocation gordonb.zziiu@burditt.org (Gordon Burditt) - 2015-07-27 22:41 -0500
    Re: lalloc() -- local stack allocation Keith Thompson <kst-u@mib.org> - 2015-07-28 08:35 -0700

csiph-web