Article View: comp.lang.c
Article #1288Re: C: a question about scope automatics in C
From: David Brown
Date: Tue, 03 Nov 2026 10:47
Date: Tue, 03 Nov 2026 10:47
71 lines
4628 bytes
4628 bytes
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/>
Technical Details
Message-ID:
<11cce4h$3m11i$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>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 11:47:44 +0100 Organization: A noiseless patient Spider Lines: 71 Message-ID: <11cce4h$3m11i$1@dont-email.me> References: <oOScndMmgZhYRXX3nZ2dnZfqnPGdnZ2d@giganews.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 03 Nov 2026 10:47:46 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3867698"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19otBpi52IxP3WoSW6eSP+kaxrSwmvfx34="; posting-host="f06b12b7403cf57442a23e3e44a23173" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:lBrWCRj7Sg/QaOskby2QPkfg4Zg= sha256:0ZwESHAsH99+fJ5RLUARmfDasKg2TDrfrF1rXjP6GjU= sha1:RfDj72M1PfFwZG9ADf/qcR8NUY4= sha256:dNSypQAaoQjfmBzFmZ5XLvdwVnsW3P2m7Ag+XazH+j8= In-Reply-To: <oOScndMmgZhYRXX3nZ2dnZfqnPGdnZ2d@giganews.com> Content-Language: en-GB, nb-NO