Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
On 07/01/2025 01:35, Alexis wrote:
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
I've used gcc's "cleanup" attribute a couple of times, typically in connection with somewhat messy stuff hidden behind simple macro names.
I think "defer" could be useful on occasion, and could sometimes give clearer and safer code than "goto"-based error handling. But as with
any such feature there is a risk of abuse - too many "defers" in a
function will make the code as much a spaghetti monster as multiple goto's. If people use it to keep code neater and reduce the risk of resource leakage, it can be a good thing. If they use it as an excuse
to write even bigger and more illegible functions, it's bad.
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
On 07.01.2025 01:35, Alexis wrote:
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
(For C++, as the author also intended, it seems, IME, to not make much sense.) For "C", inspecting the samples on that page, the most obvious application case seems to me to be the heap allocations; there I first
had the question of how it would behave e.g. with 'realloc's. But more importantly, it doesn't seem to match my typical applications of heap allocations, which I regularly don't do with just a block scope - on
On 2025-01-07, Alexis <flexibeast@gmail.com> wrote:
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
I'm okay if a self-hosted implementation can be demonstrated, which
defers itself to a future discussion.
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
Alexis <flexibeast@gmail.com> writes:
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
The issue being addressed is one well worth addressing.
The proposed solution ('defer') is awful. If this feature is
being considered for the C standard it should be rejected
out of hand.
On 08/01/2025 16:30, Tim Rentsch wrote:
Alexis <flexibeast@gmail.com> writes:
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/ >>>
What do people here think about having such a feature in C?
The issue being addressed is one well worth addressing.
What is the issue in your opinion?
The proposed solution ('defer') is awful. If this feature is
being considered for the C standard it should be rejected
out of hand.
Why?
I will tell what is the issue it solves in my opinion.
If you don't have static analysis to guide you on where to free
resources, defer helps prevent human error by ensuring resources are released properly. With defer, you only need to specify the cleanup in
one place, reducing the chances of forgetting it elsewhere. This makes
the code easier to maintain.
However, I think this problem is better addressed with static analysis, which provides stronger safety guarantees.(This is what I have done in
Cake, with static analysis)
If there's a better solution, is defer unnecessary?
I think defer still useful to write less code, to add the same
information in just one place.
Am 07.01.2025 um 01:35 schrieb Alexis:
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
That doesn’t save this hopelessly outdated programming language either.
When I dont't have a RAII-class for a special purpose in C++ I use my
class invoke_on_destruct:
Reminds me of ScopeGuard. ;^)
Alexis <flexibeast@gmail.com> writes:
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
The issue being addressed is one well worth addressing.
The proposed solution ('defer') is awful. If this feature is
being considered for the C standard it should be rejected
out of hand.
The proposed solution ('defer') is awful. ...
Am 08.01.2025 um 20:30 schrieb Tim Rentsch:
The proposed solution ('defer') is awful. ...
Why ?
Why ?
If you want destructors, use c++.
Am 09.01.2025 um 17:41 schrieb Scott Lurndal:
Why ?
If you want destructors, use c++.
If you're stuck with plain C, something like defer would
be a huge relief over gotos as used in the Linux kernel.
On 08/01/2025 20:30, Tim Rentsch wrote:
Alexis <flexibeast@gmail.com> writes:
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/
What do people here think about having such a feature in C?
The issue being addressed is one well worth addressing.
The proposed solution ('defer') is awful. If this feature is
being considered for the C standard it should be rejected
out of hand.
Jens Gustedt is not just some random C programmer - or even just some
random C book author. He is an active member of the C standards
committee, AFAIUI.
You might not agree with his suggested feature, and perhaps the rest of
the C standards committee will reject it - that's why there is a
committee, so that different ideas and suggestions can be discussed and considered from different viewpoints.
But his suggestion should /not/ be rejected out of hand. The guy has
the qualifications, and done the work, to have the right to be given
On 2025-01-09, David Brown <david.brown@hesbynett.no> wrote:
On 08/01/2025 20:30, Tim Rentsch wrote:
Alexis <flexibeast@gmail.com> writes:
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog about a
proposed `defer` feature (as provided by e.g. Zig and Go), the most
recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/ >>>>
What do people here think about having such a feature in C?
The issue being addressed is one well worth addressing.
The proposed solution ('defer') is awful. If this feature is
being considered for the C standard it should be rejected
out of hand.
Jens Gustedt is not just some random C programmer - or even just some
random C book author. He is an active member of the C standards
committee, AFAIUI.
You might not agree with his suggested feature, and perhaps the rest of
the C standards committee will reject it - that's why there is a
committee, so that different ideas and suggestions can be discussed and
considered from different viewpoints.
But his suggestion should /not/ be rejected out of hand. The guy has
the qualifications, and done the work, to have the right to be given
He has written a few macros relying on GCC features, where the real work
has been done.
That underlying GCC features is what should be standardized, if anything,
and not the proposed defer syntax:
- nested functions which have access to the parent lexicals
- the cleanup feature
- the __COUNTER__ preprocessor feature
All of these are beyond proof-of-concept, but used in production. It is
years old and mature.
What we don't want is ISO C to be reinventing any more GCC extensions,
in a different way. There is an annoying history of that.
(It's bad enough when committees just invent stuff that hasn't been implemented anywhere, but it's almost an insult when they ignore what
has been implemented and invent something incompatible.)
Note: the nested, local functions used in the presented solution are not being used as downward funargs (functional arguments): i.e. passed down
to callee functions for indirect calling. The cleanup calls take place
in the parent function frame. Thus for the purposes of these defer
macros, we don't need to specify a full blown nested function that
supports downward funargs. The standard could say that if the address
of a local function is communicated outside of the function scope where
it was taken, its value is indeterminate. Then the downard funarg
support becomes a GNU extension.
Supporting downward funargs is not a mere difficulty. The solution
chosen in GCC for local access (trampolines) has security implications:
a program which uses local functions (such as one relying on these defer macros) requires an executable stack: the virtual memory pages of its
stack segment allow code execution. This is because pointers to local functions are aimed at small pieces of dynamically generated machine
code, allocated on the stack, called trampolines. A trampoline is
co-located with the environment pointer needed by the closure; its tiny machine code sequence loads the environment pointer relative to the
program counter, and then calls the real function (which is not stack allocated). Thus, a function-with-environment which would normally
require two pointer-sized words of storage can be represented by a
simple function pointer.
On 09/01/2025 20:40, Kaz Kylheku wrote:
On 2025-01-09, David Brown <david.brown@hesbynett.no> wrote:
On 08/01/2025 20:30, Tim Rentsch wrote:
Alexis <flexibeast@gmail.com> writes:
Hi all,
"Modern C" author Jens Gustedt has been posting on his blog
about a proposed `defer` feature (as provided by e.g. Zig and
Go), the most recent being:
https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/ >>>>
What do people here think about having such a feature in C?
The issue being addressed is one well worth addressing.
The proposed solution ('defer') is awful. If this feature is
being considered for the C standard it should be rejected
out of hand.
Jens Gustedt is not just some random C programmer - or even just
some random C book author. He is an active member of the C
standards committee, AFAIUI.
You might not agree with his suggested feature, and perhaps the
rest of the C standards committee will reject it - that's why
there is a committee, so that different ideas and suggestions can
be discussed and considered from different viewpoints.
But his suggestion should /not/ be rejected out of hand. The guy
has the qualifications, and done the work, to have the right to be
given
He has written a few macros relying on GCC features, where the real
work has been done.
That underlying GCC features is what should be standardized, if
anything, and not the proposed defer syntax:
- nested functions which have access to the parent lexicals
- the cleanup feature
- the __COUNTER__ preprocessor feature
All of these are beyond proof-of-concept, but used in production.
It is years old and mature.
Yes, standardising these would be a good thing for C IMHO. (I seem
to recall __COUNTER__ being standardised, but I'm not sure on that.)
I believe Jens Gustedt has also proposed a form of lambdas for C -
that would work as an alternative to gcc's nested functions.
However such local functions end up if and when they get
standardised, I think it is not unreasonable if there are
restrictions that block any usage that requires passing around data
in addition to the function. That is to say, you should not be able
to pass on a pointer to a local function that has captures - anything
that needs a gcc nested function "trampoline" or a C++ lambda style
function object should not be allowed. That would still leave full flexibility for local use of local functions -
such as used here for
the "defer" macros - as well as supporting convenient local functions
for things like qsort comparison functions.
I don't find the 'defer' proposal readable and I suspect it
may lead to more maintainability issues that simple gotos.
Am 09.01.2025 um 19:23 schrieb Scott Lurndal:
I don't find the 'defer' proposal readable and I suspect it
may lead to more maintainability issues that simple gotos.
It has the same readability like a scope_guard.
Bonita Montero <Bonita.Montero@gmail.com> writes:
Am 09.01.2025 um 17:41 schrieb Scott Lurndal:
Why ?
If you want destructors, use c++.
If you're stuck with plain C, something like defer would
be a huge relief over gotos as used in the Linux kernel.
Most C-code can be compiled with a C++ compiler, and
one can selectively use certain C++ features, such as
destructors.
I don't find the 'defer' proposal readable and I suspect it
may lead to more maintainability issues that simple gotos.
Bonita Montero <Bonita.Montero@gmail.com> writes:
Am 09.01.2025 um 19:23 schrieb Scott Lurndal:
I don't find the 'defer' proposal readable and I suspect it
may lead to more maintainability issues that simple gotos.
It has the same readability like a scope_guard.
Thank you for making my point.
Am 08.01.2025 um 23:39 schrieb Chris M. Thomasson:
Reminds me of ScopeGuard. ;^)
But better than this. If you have multiple changes on a data structure
and the last fails the invoke_on_destruct-s are chained and revert the changes incrementally.
Bonita Montero <Bonita.Montero@gmail.com> writes:
Am 09.01.2025 um 17:41 schrieb Scott Lurndal:
Why ?
If you want destructors, use c++.
If you're stuck with plain C, something like defer would
be a huge relief over gotos as used in the Linux kernel.
Most C-code can be compiled with a C++ compiler, [...]
I don't find the 'defer' proposal readable and I suspect it
may lead to more maintainability issues that simple gotos.
I don't find the 'defer' proposal readable and I suspect it
may lead to more maintainability issues that simple gotos.
Instead of calling it 'defer' it should be called 'goto++'.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,007 |
Nodes: | 10 (0 / 10) |
Uptime: | 196:36:37 |
Calls: | 13,143 |
Files: | 186,574 |
D/L today: |
286 files (72,957K bytes) |
Messages: | 3,310,128 |