Article View: comp.lang.c
Article #1304Re: C: a question about scope automatics in C
From: David Brown
Date: Tue, 03 Nov 2026 16:08
Date: Tue, 03 Nov 2026 16:08
99 lines
5790 bytes
5790 bytes
On 03/11/2026 16:14, Ross Finlayson wrote: > 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.) > It didn't help. Your writing is absolute tripe. I have no idea what you are trying to say, or what you are trying to ask - I can't even tell if you are actually asking anything. If the information about gcc's "cleanup" attribute and the macros and C2y "defer" proposal helps you, that's great. But if you can't write understandable text, there is no point in discussing further. Writing sixty words per minute is useless when the words don't convey information. Reading "a million miles per hour" is useless when you do not take in the content of what you are reading.
Technical Details
Message-ID:
<11cd0tn$3tbkg$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<oOScndMmgZhYRXX3nZ2dnZfqnPGdnZ2d@giganews.com> <11cce4h$3m11i$1@dont-email.me> <8V2dnW6M3YIeZ3T3nZ2dnZfqn_SdnZ2d@giganews.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: C: a question about scope automatics in C Date: Tue, 3 Nov 2026 17:08:23 +0100 Organization: A noiseless patient Spider Lines: 99 Message-ID: <11cd0tn$3tbkg$1@dont-email.me> References: <oOScndMmgZhYRXX3nZ2dnZfqnPGdnZ2d@giganews.com> <11cce4h$3m11i$1@dont-email.me> <8V2dnW6M3YIeZ3T3nZ2dnZfqn_SdnZ2d@giganews.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 03 Nov 2026 16:08:24 +0000 (UTC) Injection-Info: dont-email.me; logging-data="4107920"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Hi13fyS+8Zb5TEL2ft7svgUhH/B/MU+E="; posting-host="f06b12b7403cf57442a23e3e44a23173" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:yPeTfyDMKlikHVFwHsM7KxIqVCw= sha256:M4XjY/TKCcOfEamvq3YDhhLUFQ4yeT7Zz+wEtCqfGS4= sha1:d3BN4Swks9JFbqks3Y9dukCFxkU= sha256:yplPv3/ANgacLKNJGwnjJvDpMixEMq20efZ9SZgN+yI= Content-Language: en-GB, nb-NO In-Reply-To: <8V2dnW6M3YIeZ3T3nZ2dnZfqn_SdnZ2d@giganews.com>