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


Groups > comp.lang.c > #58822

Re: local arrays and stack

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: local arrays and stack
Date 2015-03-24 09:55 +0100
Organization A noiseless patient Spider
Message-ID <mer8p1$7tk$1@dont-email.me> (permalink)
References (6 earlier) <mepdmh$qso$1@dont-email.me> <87fv8vu1sx.fsf@kst-u.example.com> <mepfa1$26r$1@dont-email.me> <1f3963cb-74d2-4b38-9241-fd6ce6c5179c@googlegroups.com> <mepjd8$kb1$1@dont-email.me>

Show all headers | View raw


On 23/03/15 18:44, Bartc wrote:
> On 23/03/2015 17:12, Xcott Craver wrote:
>> On Monday, March 23, 2015 at 12:35:07 PM UTC-4, Bart wrote:
>>>
>>> I don't know what gcc in particular does. But if allocating a VLA,
>>> perhaps inside a block which is the body of a loop, and the size is
>>> different each time, then wouldn't it have to take of such details
>>> anyway? Including dealing with goto, break, longjmp and return.
>>
>> If doing this on the stack, allocating a VLA means decrementing the
>> stack pointer, and deallocating it means resetting the stack pointer
>> to a previous value.
> 
> How? The VLA size is a runtime expression. If it's a complicated one,
> then it would need to be stored somewhere (presumably also on the
> stack), as it would be awkward to push the stack pointer itself.

Just assume that the VLA is allocated with alloca() rather than malloc()
- with all the advantages and disadvantages that implies.  Ideally, this
means copying the SP to the BP register (so that the compiler can access
data already on the stack with BP+offset after SP changes), then change
SP to make enough space on the stack for the VLA.  Deallocation is done
by copying the BP back into the SP.  This is exactly the same procedure
as used whenever a function needs a full stack frame (rather than the
simplified versions typically used these days).

Jumping around inside the function (goto, break, etc.) or leaving the
function is handled just like normal.  Longjmp and setjmp are messy as
always, but not worse than usual.

> 
> See, already it's a bit more elaborate than it looks at first ...
> 
>> Even if you are declaring an array in a loop with a different length
>> each time, the allocation code should not require anything more
>> complicated than altering a register at the beginning of the loop,
>> and altering it again at the end.
> 
> ... and it can get worse:
> 
> With gcc, local arrays with a fixed size over about 4KB involve a
> function call to ensure the remaining stack is big enough (so that there
> is no page fault).

That will depend on the target and the OS - maybe Windows requires some
sort of check like that.  In general, however, a compiler will simply
pretend that the stack is as big as needed and rely on the OS and MMU to
sort out the details (such as killing the program if it breaks the rules
of the OS).

> 
> What about VLAs over 4KB? The trouble is it won't know whether a VLA is
> bigger than 4KB, and whether to call that __chkstk_ms() function, until
> runtime. Now it gets complicated enough that using malloc()/free() might
> be a simpler solution.
> 

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


Thread

local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-22 01:51 -0700
  local arrays and stack asetofsymbols@gmail.com - 2015-03-22 02:23 -0700
  Re: local arrays and stack Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-22 13:22 -0700
    Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 01:55 -0700
  Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-22 15:07 -0700
    Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 01:50 -0700
      Re: local arrays and stack Robert Wessel <robertwessel2@yahoo.com> - 2015-03-23 04:26 -0500
        Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 06:53 -0700
        Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 07:29 -0700
          Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 08:34 -0700
          Re: local arrays and stack Robert Wessel <robertwessel2@yahoo.com> - 2015-03-23 19:57 -0500
          Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-24 04:01 -0700
            Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-24 04:37 -0700
            Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 13:04 +0100
              Re: local arrays and stack glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-24 17:29 +0000
                Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 20:01 +0100
        Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-23 08:24 -0700
          Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 08:42 -0700
            Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-23 16:07 +0000
              Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 09:15 -0700
              Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-23 09:16 -0700
                Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-23 16:34 +0000
                Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 10:12 -0700
                Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-23 17:44 +0000
                Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 11:47 -0700
                Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 12:06 -0700
                Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-23 20:25 +0000
                Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 14:10 -0700
                Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-23 22:22 +0000
                Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 23:57 -0700
                Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 10:28 +0100
                Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-24 10:44 +0000
                Re: local arrays and stack Martin Shobe <martin.shobe@yahoo.com> - 2015-03-24 06:38 -0500
                Re: local arrays and stack Martin Shobe <martin.shobe@yahoo.com> - 2015-03-24 09:57 -0500
                Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 13:22 +0100
                Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-25 16:07 +0000
                Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-25 17:42 +0100
                Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-25 19:06 +0000
                Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-25 12:26 -0700
                Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-25 13:20 -0700
                Re: local arrays and stack Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-25 12:28 -0700
                Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-25 21:45 +0100
                Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-25 12:50 -0700
                Re: local arrays and stack Bartc <bc@freeuk.com> - 2015-03-25 22:33 +0000
                Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-25 23:57 +0100
                Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-25 21:42 +0100
                Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-25 09:55 -0700
                Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-25 12:36 -0700
                Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-25 13:04 -0700
                Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-25 21:52 +0100
                Re: local arrays and stack Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-25 15:18 -0700
                Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-26 00:02 +0100
                Re: local arrays and stack Melzzzzz <mel@zzzzz.com> - 2015-03-26 00:47 +0100
                Re: local arrays and stack Mel <mel@zzzzz.com> - 2015-03-24 14:58 +0100
                Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-24 06:58 -0700
                Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-23 14:34 -0700
                Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 10:16 +0100
                Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 09:55 +0100
                Re: local arrays and stack Noob <root@127.0.0.1> - 2015-03-24 10:23 +0100
                Re: local arrays and stack Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 12:18 -0700
                Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-24 13:42 -0700
                Re: local arrays and stack Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 08:47 -0700
            Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 09:32 -0700
              Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-23 09:57 -0700
              Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-23 10:37 -0700
                Re: local arrays and stack Xcott Craver <xcottcraver@gmail.com> - 2015-03-23 10:49 -0700
                Re: local arrays and stack Keith Thompson <kst-u@mib.org> - 2015-03-23 12:48 -0700
                Re: local arrays and stack jt@toerring.de (Jens Thoms Toerring) - 2015-03-23 22:29 +0000
                Re: local arrays and stack James Kuyper <jameskuyper@verizon.net> - 2015-03-23 19:03 -0400
                Re: local arrays and stack jt@toerring.de (Jens Thoms Toerring) - 2015-03-23 23:36 +0000
                Re: local arrays and stack fir <profesor.fir@gmail.com> - 2015-03-24 02:17 -0700
                Re: local arrays and stack Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-23 11:56 -0700
          Re: local arrays and stack Robert Wessel <robertwessel2@yahoo.com> - 2015-03-23 20:00 -0500
            Re: local arrays and stack David Brown <david.brown@hesbynett.no> - 2015-03-24 10:39 +0100

csiph-web