🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1298

Re: C: a question about scope automatics in C

#1298
From: Ross Finlayson
Date: Tue, 03 Nov 2026 15:14
227 lines
10718 bytes
On 11/03/2026 02:47 AM, David Brown wrote:
> On 02/11/2026 19:38, Ross Finlayson wrote:
>>
>> C: a question about scope automatics in C
>>
>> Hello, I hope someone can tell me whether C may make
>> for scope automatics any account of attaching side-effects
>> to block-declared variables about scope-exit of automatics
>> making for any way in C to attach behavior to scope-exit,
>> or any way to make for when a variable goes out of scope
>> (block-scope, or, function-scope) to make any account of
>> cleanup of it. C++ of course has that's the usual way of
>> things with stack-allocation and block-scope for automatics,
>> I'm wondering if there's any way C makes it so (I think not,
>> yet would be glad to know so).
>>
>>
>> If not, are there common compiler extensions to so indicate
>> when a variable goes out of scope and make effects on it?
>>
>> Thanks for reading, also I'd be interested your opinion.
>>
>
> As has been pointed out by several people, if you want helpful answers,
> you will have to make a lot more effort to write clear, grammatical
> English.  Most of what you write is barely comprehensible.  Try thinking
> before you write, split text into sentences and paragraphs, and then
> re-read what you have written.  Maybe it would help if you give some
> examples in C.
>
> It would also be useful if you tried to stay up to date with modern C
> standards, but of course it is fine to ask about the details.  Most C
> programmers don't read the standards or proposals, but there is no point
> in trying to invent "new" features if they are already in the language,
> or likely to come in the near future.
>
> And please do not post extracts from some AI chatbot.  If you want to
> use these tools as a source of information, that's up to you - they can
> be useful research assistants, but are often inaccurate.  (I only read a
> little of your "GG" post, and there was a lot wrong in it.)  People in a
> group like comp.lang.c are here to talk with, listen to, learn from and
> help other people.  If we want AI slop, we know where to find it ourselves.
>
>
> As to the particular matter in hand - running some sort of clean-up code
> when a local variable's lifetime ends - you should first understand that
> it is the variable's /lifetime/ that is important, not its scope.  For
> local variables that are not declared "static", these are closely
> related.  But "scope" is about when the identifier's name can be used,
> "lifetime" is when the object itself exists.
>
> For gcc (and clang, and perhaps other compilers), you can use the
> "cleanup" attribute to run code at the end of the lifetime of an
> automatic storage duration variable.  (The gcc documentation incorrectly
> says "scope", and gives no information about complicated situations such
> as using "goto" or "longjmp".)
>
> <https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-cleanup>
>
>
> For the future, it is likely that C will gain a "defer" feature in the
> next C standard (currently known as C2y).  Macros to get an approximate
> version of this are available, using gcc extensions.  Here is a simple
> one :
>
> <https://antonz.org/defer-in-c/>
>
> More advanced versions can be found on Jens Gustedt's blog :
>
> <https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/>
> <https://gustedt.wordpress.com/2026/02/15/defer-available-in-gcc-and-clang/>
>
>
>


It's appreciated about "cleanup" and "defer" as
with regards to instrumenting code at entry and exit,
of function-scope and block-scope.

Hm. Maybe a story will help. (Maybe it won't.)

So, let's say we've all been around programming
since at least three decades ago, and we're all
pretty familiar with C and POSIX, and about machine
architecture, and about the usual ways operating systems
make for the execution the process model, and about
the model of memory.


So, a most familiar sort of thing in C is malloc & free,
vis-a-vis, local variables, which are automatic or auto
in storage-class, about the lifetime of objects according
to their types and storage-class.

Then, about temporaries, here local variables that are
just declared then initialized as defined to bridge the
gap between a value (or, structure, i.e., struct or array)
of one type and value (or structure) of another type,
if they're structures and not to be particularly large,
so that the code-segment isn't particularly large,
and to be re-entrant, so not in the data-segment,
then a usual idea is to allocate them dynamically,
eg with malloc & free, memory-allocating buffers
before use and free'ing them after use.

So, malloc and free are system-calls, in the implementation
or the runtime or the O.S., and so when the structure is
already in the code or data-segment, and needn't be
re-entrant, then these system-calls can be reduced.


There's a usual idea that the code that's:

alias-less
branch-less
stall-less
call-less
fault-less

has that each these alias/branch/stall/call/fault
in the processor: get in the way of the straight-line
sort of full pipeline of the processor.

So, avoiding system-calls, when it's possible,
i.e. that it remains correct, may make for
improved performance, about things like:

correctness
performance
diagnosibility
maintainability
robustness

and so on, tthe runtime side of "quality".



So, one account of correctness, is error-handling or
exception-handling, and, another is "failure modes".
A usual idea in distributed is that failure modes
have that the robustness of the program besides
the algorithm, is mostly that people design programs
that failures are fatal and the programs just exit,
vis-a-vis the algorithm and the data at rest, or
for "no drops & no dupes", and the "eventually consistent"
and "self-healing", ideas from distributed systems. So,
I'm mentioning this, because my goal is robust programs
besides, because eventually someone's going to want
a program that if they send it a signal that it doesn't
just die, and that a signal could arrive at an inopportune
time, that basically "cleanup" and "defer", for example,
would never be invoked, then if it was in the condition
where memory was malloc'ed and not free'd, that would
be a leak, which, over time, would equal waste and perhaps
also failure.

So, I want the flexibility and efficiency of "dynamic allocation",
so, not a waste of the space in the code-segment or data-segment,
which might not be re-entrant anyways, and the robustness for
correctness of cleanup or as after stack-unwinding. Then, where
that's at already is in the stack-segment, since according to the
process-model and thusly also of threads these implementers
of runtimes, the stack-segment or thread-stack-segment is
a small, fixed amount of memory that may be divvied up by
the process itself for its own purposes as a model of dynamic
allocation: with the property that if the function exits, like
if a signal arrives: it's the only thing the operating system can
give to prevent leaks of otherwise "dynamically" allocated data,
without the usual failure mode of the fatal error and killing
the entire process and reclaiming all its resources.


Then, there are some more needful's of the sources.

portable
re-usable
un-cluttered
idiomatic, not necessarily yet usually
"clean", though clutter as attribution vis-a-vis "fighting the interface"

As well, there are needful's of the libraries, or re-usable code.
These are usually enough matters of "library design" with
regards to principle-of-least-atonishment usually enough
and "3'rd party code" and "dependency management",
about "DLL Hell" and "Jar Hell" and the like, yet mostly about
the process model and the resource model and again part of "quality".




So, when dealing with dynamic allocations, there are variously
long-lived or short-lived allocations, and, variously, of fixed-size
or variable-size, or length.


Then, these "automatic temporary arrays", are short-lived, vis-a-vis,
"buffers" usually enough more generally, which may be the destinations
of other process' lifecycles in the O.S. I/O, the operating-system's
account of input/output.


So, I figure anybody who bothers reading this "knows" all these
things, and they're not even un-usual, in usual sorts of accounts
of the thorough.




Then, about the "A.I. chat-bot output and shove off with that",
that's agreeable, myself I type about sixty words per minute
yet read about a million miles per hour, and I'll read it critically
anyways, then that sort of thing is take-it-or-leave-it.


So, basically since "C" doesn't say otherwise, about the lifeycle
and duration of object storage, then it's figured that a "least-ledge
automatic temporary arrays", can do some work before the compiler
starts after it parses the compilation units, and figure that these
"least-ledge automatic temporary arrays", can live in a very brief
form in the stack-segment, while the source code looks un-cluttered,
for example to make non-re-entrant code into re-entrant code for
the multi-threaded server applications, without changing the code
(where it fits).


Technical Details
Message-ID: <8V2dnW6M3YIeZ3T3nZ2dnZfqn_SdnZ2d@giganews.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.nrbbs.org!border-3.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-4.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
References: <oOScndMmgZhYRXX3nZ2dnZfqnPGdnZ2d@giganews.com> <11cce4h$3m11i$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.nrbbs.org!border-3.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-4.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 03 Nov 2026 15:15:15 +0000
Subject: Re: C: a question about scope automatics in C
Newsgroups: comp.lang.c
References: <oOScndMmgZhYRXX3nZ2dnZfqnPGdnZ2d@giganews.com>
 <11cce4h$3m11i$1@dont-email.me>
From: Ross Finlayson <ross.a.finlayson@gmail.com>
Date: Tue, 3 Nov 2026 07:14:57 -0800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101
 Thunderbird/38.6.0
MIME-Version: 1.0
In-Reply-To: <11cce4h$3m11i$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <8V2dnW6M3YIeZ3T3nZ2dnZfqn_SdnZ2d@giganews.com>
Lines: 227
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-PlZKP8M7/MFnv6nrPVEvIofUL7+xX5Wle4LMsAmlRgTzAKNPbZIrbWDqfHnD0j7E9bJtYwLDzU5ZvNH!KbM8YHGZGxVHPBuZuT8LZqB7hzRGM6Bvw7LBcTgAfKaTE2jpTMxiXMBWNb4LbrXue8FJt8umqyk=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40