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


Groups > comp.lang.c > #399439

Re: Using gotos with local variables in "extra" blocks: Does it get deallocated?

From cross@spitfire.i.gajendra.net (Dan Cross)
Newsgroups comp.lang.c
Subject Re: Using gotos with local variables in "extra" blocks: Does it get deallocated?
Date 2026-05-26 14:13 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <10v49pt$3fq$2@reader1.panix.com> (permalink)
References <10v1jrr$35gqh$2@news.xmission.com> <10v1toh$qrb$1@reader1.panix.com> <DY%QR.370372$gO1.309583@fx14.iad>

Show all headers | View raw


In article <DY%QR.370372$gO1.309583@fx14.iad>,
Scott Lurndal <slp53@pacbell.net> wrote:
>cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>In article <10v1jrr$35gqh$2@news.xmission.com>,
>>Kenny McCormack <gazelle@shell.xmission.com> wrote:
>>>Trigger Warning: This post contains the dreaded "s word".  Viewer
>>>discretion advised.
>>>
>>>Based on some recent threads (mostly, the Bart vs. The World ones), and
>>>also on some of my own code, I got to wondering about the following code
>>>fragment:
>>>
>>>    void foo(void) {
>>>	/* stuff */
>>>	{	/* Enter an "extra" block */
>>>	int i = 42;	/* 'i' is allocated on the stack */
>>>	/* stuff */
>>>	if (someCond) goto OutOfHere;
>>>	/* more stuff */
>>>	}	/* End of "extra" block */
>>>    OutOfHere: puts("Got to OutOfHere");
>>>    }	/* End of foo() */
>>>
>>>Now suppose someCond is true and the branch is taken.  Does the variable
>>>'i' get deallocated (i.e., the stack pointer re-adjusted) ?
>>>
>>>Is this guaranteed to work?
>>
>>The variable `i` will be deallocated, yes.  But if the question
>>is whether that happens immediately after the `goto` or not,
>>then it's that's a different question.
>>
>>The standard says that if a variable with "automatic storage
>>duration" is not a VLA (which describes `i` in this case), then
>>the lifetime of the object ends when "execution of that block
>>ends in any way" (sec 6.2.4 of the various standards; para 6 in
>>n3220).
>>
>>Here, "lifetime" is defined as, "the portion of program
>>execution during which storage is guaranteed to be reserved for
>>it."
>>
>>So whether or not the storage is _immediately_ reclaimed is
>>another matter; it may be immediately deallocated after exiting
>>the block, or when the function returns, or some other time.
>>
>>>Note that in most/all "scripting" languages, this code pattern would be a
>>>big no-no, and if you did it enough times, it will blow up the program.
>>
>>Huh.  Which languages?
>>
>>>Note, BTW, that we could make this issue go away by putting OutOfHere a
>>>line earlier - i.e., inside the "extra" block - but then we'd run into
>>>Bart's issue (i.e., we'd have to put an extra ';' after the label).
>>
>>It actually doesn't change the issue at all; whether "execution"
>>of the block ends due to a `goto` out of it or reaching the end
>>of the block as written is irrelevant: it'll be deallocated
>>regardless.  But whether that happens immediately or not is
>>another matter.
>>
>>Assuming a stack-based implementation, I'd assume most compilers
>>would deallocate it when the enclosing function returns.
>
>In believe in most cases, the compiler simply adds the required
>storage space for all variables defined in inner-blocks to the
>initial stack allocation on function entry. Which is all "deallocated"
>when the function returns.  Indeed, I believe that the storage
>reserved for inner block variables can be repurposed and used
>in subsequent inner-blocks that have local variables in the same
>function. 

Almost certainly, yes.  Of course there are some exceptions, but
the vast bulk of real-world implementations will use a stack and
likely do exactly what you just said.  And on exit, deallocating
a stack frame may as simple as a single instruction.

	- Dan C.

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


Thread

Using gotos with local variables in "extra" blocks: Does it get deallocated? gazelle@shell.xmission.com (Kenny McCormack) - 2026-05-25 13:46 +0000
  Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-05-25 16:19 +0200
  Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Bart <bc@freeuk.com> - 2026-05-25 15:21 +0100
    The C23 thing (Was: Using gotos with local variables in "extra" blocks: Does it get) deallocated? gazelle@shell.xmission.com (Kenny McCormack) - 2026-05-25 14:32 +0000
    Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-05-25 16:34 +0200
  Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Andrew Smallshaw <andrews@sdf.org> - 2026-05-25 16:09 +0000
    The whole point of this NG is worrying about "UB" (Was: Using gotos with local variables in "extra" blocks: Does it get) deallocated? gazelle@shell.xmission.com (Kenny McCormack) - 2026-05-25 16:54 +0000
      Re: The whole point of this NG is worrying about "UB" (Was: Using gotos with local variables in "extra" blocks: Does it get) deallocated? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-05-25 19:08 +0200
        Re: The whole point of this NG is worrying about "UB" (Was: Using gotos with local variables in "extra" blocks: Does it get) deallocated? gazelle@shell.xmission.com (Kenny McCormack) - 2026-05-25 17:23 +0000
          Re: The whole point of this NG is worrying about "UB" (Was: Using gotos with local variables in "extra" blocks: Does it get) deallocated? Bart <bc@freeuk.com> - 2026-05-25 18:48 +0100
  Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? cross@spitfire.i.gajendra.net (Dan Cross) - 2026-05-25 16:35 +0000
    Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? scott@slp53.sl.home (Scott Lurndal) - 2026-05-25 17:49 +0000
      Variables in inner blocks (was: Using gotos with local variables in "extra" blocks: Does it get deallocated?) pa@see.signature.invalid (Pierre Asselin) - 2026-05-25 19:52 +0000
      Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? cross@spitfire.i.gajendra.net (Dan Cross) - 2026-05-26 14:13 +0000
        Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? scott@slp53.sl.home (Scott Lurndal) - 2026-05-26 17:10 +0000
          Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? David Brown <david.brown@hesbynett.no> - 2026-05-26 20:31 +0200
          Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-05-31 16:24 -0700
            Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-05-31 16:36 -0700
  Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? David Brown <david.brown@hesbynett.no> - 2026-05-25 18:40 +0200
  Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-05-26 17:14 -0500
    Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? cross@spitfire.i.gajendra.net (Dan Cross) - 2026-05-27 00:23 +0000
      Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? gazelle@shell.xmission.com (Kenny McCormack) - 2026-05-27 02:09 +0000
      Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Lynn McGuire <lynnmcguire5@gmail.com> - 2026-05-26 22:51 -0500
        Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-05-27 04:46 +0000
          Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? David Brown <david.brown@hesbynett.no> - 2026-05-27 09:21 +0200
        Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? cross@spitfire.i.gajendra.net (Dan Cross) - 2026-05-27 12:40 +0000
          Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? scott@slp53.sl.home (Scott Lurndal) - 2026-05-27 14:48 +0000
          Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-27 13:39 -0700
            Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-05-27 17:04 -0700
              Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-27 18:16 -0700
          Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? antispam@fricas.org (Waldek Hebisch) - 2026-05-28 00:35 +0000
            Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2026-05-29 04:01 +0100
        Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-27 13:32 -0700
  Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? fir <profesor.fir@gmail.com> - 2026-05-27 15:33 +0200
    Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Bart <bc@freeuk.com> - 2026-05-27 17:18 +0100
      Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? fir <profesor.fir@gmail.com> - 2026-05-27 18:51 +0200
        Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? fir <profesor.fir@gmail.com> - 2026-05-27 18:56 +0200
    Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-05-27 22:42 +0000
      Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? fir <profesor.fir@gmail.com> - 2026-05-28 07:33 +0200
        Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-05-28 05:37 +0000
          Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? fir <profesor.fir@gmail.com> - 2026-05-28 17:35 +0200
            Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? Bart <bc@freeuk.com> - 2026-05-28 16:49 +0100
              Re: Using gotos with local variables in "extra" blocks: Does it get deallocated? fir <profesor.fir@gmail.com> - 2026-05-28 18:04 +0200

csiph-web