frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Defer available in gcc and clang

https://gustedt.wordpress.com/2026/02/15/defer-available-in-gcc-and-clang/
57•r4um•4d ago

Comments

kjgkjhfkjf•1h ago
The article is a bit dense, but what it's announcing is effectively golang's `defer` (with extra braces) or a limited form of C++'s RAII (with much less boilerplate).

Both RAII and `defer` have proven to be highly useful in real-world code. This seems like a good addition to the C language that I hope makes it into the standard.

Zambyte•1h ago
Probably closer to defer in Zig than in Go, I would imagine. Defer in Go executes when the function deferred within returns; defer in Zig executes when the scope deferred within exits.
babalark•1h ago
Yes!! One step closer to having defer in the standard.

Related blog post from last year: https://thephd.dev/c2y-the-defer-technical-specification-its... (https://news.ycombinator.com/item?id=43379265)

LexiMax•1h ago
A long overdue feature.

Though I do wonder what the chances are that the C subset of C++ will ever add this feature. I use my own homespun "scope exit" which runs a lambda in a destructor quite a bit, but every time I use it I wish I could just "defer" instead.

Panzerschrek•1h ago
Such addition is great. But there is something even better - destructors in C++. Anyone who writes C should consider using C++ instead, where destructors provide a more convenient way for resources freeing.
alextingle•56m ago
C++ destructors are implicit, while defer is explicit.

You can just look at the code in front of you to see what defer is doing. With destructors, you need to know what type you have (not always easy to tell), then find its destructor, and all the destructors of its parent classes, to work out what's going to happen.

Sure, if the situation arises frequently, it's nice to be able to design a type that "just works" in C++. But if you need to clean up reliably in just this one place, C++ destructors are a very clunky solution.

mathisfun123•56m ago
i write C++ every day (i actually like it...) but absolutely no one is going to switch from C to C++ just for dtors.
Pay08•42m ago
Weren't dtors the reason GCC made the switch?
amluto•36m ago
I think destructors are different, not better. A destructor can’t automatically handle the case where something doesn’t need to be cleaned up on an early return until something else occurs. Also, destructors are a lot of boilerplate for a one-off cleanup.
Someone•17m ago
For the cases where a destructor isn’t readily available, you can write a defer class that runs a lambda passed to its constructor in its destructor, can’t you?

Would be a bit clunky, but that can (¿somewhat?) be hidden in a macro, if desired.

jonhohle•1h ago
It’s pedantic, but in the malloc example, I’d put the defer immediately after the assignment. This makes it very obvious that the defer/free goes along with the allocation.

It would run regardless of if malloc succeeded or failed, but calling free on a NULL pointer is safe (defined to no-op in the C-spec).

userbinator•55m ago
As others have commented already: if you want to use C++, use C++. I suspect the majority of C programmers neither care nor want stuff like this; I still stay with C89 because I know it will be portable anywhere, and complexities like this are completely at odds with the reason to use C in the first place.
laserbeam•27m ago
I would say the complexity of implementing defer yourself is a bit annoying for C. However defer itself, as a language feature in a C standard is pretty reasonable. It’s a very straightforward concept and fits well within the scope of C, just as it fit within the scope of zig. As long as it’s the zig defer, not the golang one…

I would not introduce zig’s errdeferr though. That one would need additional semantics changes in C to express errors.

qsera•10m ago
>pretty reasonable

It starts out small. Then before you know the language is total shit. Python is a good example.

I am observing a very distinguishable phenomenon when internet makes very shallow ideas mainstream and ruin many many good things that stood the test of time.

I am not saying this is one of those instances, but what the parent comment makes sense to me. You can see another comment who now wants to go further and want destructors in C. Because of internet, such voices can now reach out to each other, gather and cause a change. But before, such voices would have to go through a lot of sensible heads before they would be able to reach each other. In other words, bad ideas got snuffed early before internet, but now they go mainstream easily.

So you see, it starts out slow, but then more and more stuff gets added which diverges more and more from the point.

majke•10m ago
Not necessarily. In classic C we often build complex state machines to handle errors - especially when there are many things that need to be initialized (malloced) one after another and each might fail. Think the infamous "goto error".

I think defer{} can simplify these flows sometimes, so it can indeed be useful for good old style C.

Mond_•10m ago
I think a lot of the really old school people don't care, but a lot of the younger people (especially those disillusioned with C++ and not fully enamored with Rust) are in fact quite happy for C to evolve and improve in conservative, simple ways (such as this one).
gignico•28m ago
I’m just going to start teaching classes of C programming to university first-year CS students. Would you teach `defer` straight away to manage allocated memory?
leni536•9m ago
It's still only in a TS, not in ISO C, if that matters.

Defer available in gcc and clang

https://gustedt.wordpress.com/2026/02/15/defer-available-in-gcc-and-clang/
62•r4um•4d ago•19 comments

Consistency diffusion language models: Up to 14x faster, no quality loss

https://www.together.ai/blog/consistency-diffusion-language-models
53•zagwdt•3h ago•9 comments

Gemini 3.1 Pro

https://blog.google/innovation-and-ai/models-and-research/gemini-models/gemini-3-1-pro/
683•MallocVoidstar•16h ago•769 comments

An AI Agent Published a Hit Piece on Me – The Operator Came Forward

https://theshamblog.com/an-ai-agent-wrote-a-hit-piece-on-me-part-4/
293•scottshambaugh•4h ago•227 comments

Pi for Excel: AI sidebar add-in for Excel

https://github.com/tmustier/pi-for-excel
45•rahimnathwani•5h ago•7 comments

Reading the undocumented MEMS accelerometer on Apple Silicon MacBooks via iokit

https://github.com/olvvier/apple-silicon-accelerometer
19•todsacerdoti•2h ago•4 comments

Show HN: Micasa – track your house from the terminal

https://micasa.dev
514•cpcloud•15h ago•167 comments

An ARM Homelab Server, or a Minisforum MS-R1 Review

https://sour.coffee/2026/02/20/an-arm-homelab-server-or-a-minisforum-ms-r1-review/
53•neelc•5h ago•44 comments

Almost Every infrastructure decision I endorse or regret after 4 years

https://cep.dev/posts/every-infrastructure-decision-i-endorse-or-regret-after-4-years-running-inf...
173•Meetvelde•3d ago•82 comments

Mystery donor gives Japanese city $3.6M in gold bars to fix water system

https://www.bbc.com/news/articles/c3ew5jlqz87o
66•tartoran•2h ago•30 comments

FreeCAD

https://www.freecad.org/index.php
43•doener•2d ago•17 comments

Show HN: Fostrom, an IoT Cloud Platform built for developers

https://fostrom.io/
3•arjunbajaj•1h ago•0 comments

US plans online portal to bypass content bans in Europe and elsewhere

https://www.reuters.com/world/us-plans-online-portal-bypass-content-bans-europe-elsewhere-2026-02...
265•c420•1d ago•361 comments

A beginner's guide to split keyboards

https://www.justinmklam.com/posts/2026/02/beginners-guide-split-keyboards/
95•thehaikuza•4d ago•105 comments

Micropayments as a reality check for news sites

https://blog.zgp.org/micropayments-as-a-reality-check-for-news-sites/
151•speckx•11h ago•322 comments

America vs. Singapore: You can't save your way out of economic shocks

https://www.governance.fyi/p/america-vs-singapore-you-cant-save
267•guardianbob•16h ago•394 comments

Fast KV Compaction via Attention Matching

https://arxiv.org/abs/2602.16284
6•cbracketdash•2h ago•0 comments

A terminal weather app with ASCII animations driven by real-time weather data

https://github.com/Veirt/weathr
204•forinti•13h ago•35 comments

MuMu Player (NetEase) silently runs 17 reconnaissance commands every 30 minutes

https://gist.github.com/interpiduser5/547d8a7baec436f24b7cce89dd4ae1ea
245•interpidused•5h ago•98 comments

Paged Out Issue #8 [pdf]

https://pagedout.institute/download/PagedOut_008.pdf
366•SteveHawk27•19h ago•57 comments

A Famous Enigma: On Alexandre Kojève

https://clereviewofbooks.com/isabel-jacobs-boris-groys-marco-filoni/
8•Caiero•1d ago•0 comments

Lindenmayer.jl: Defining recursive patterns in Julia

https://cormullion.github.io/Lindenmayer.jl/stable/
42•WillMorr•3d ago•2 comments

Archaeologists find possible first direct evidence of Hannibal's war elephants

https://www.smithsonianmag.com/smart-news/archaeologists-unearthed-a-2200-year-old-bone-they-say-...
100•bryanrasmussen•12h ago•27 comments

Pebble Production: February Update

https://repebble.com/blog/february-pebble-production-and-software-updates
277•smig0•18h ago•130 comments

Show HN: A small, simple music theory library in C99

https://github.com/thelowsunoverthemoon/mahler.c
31•lowsun•8h ago•7 comments

Show HN: Ghostty-based terminal with vertical tabs and notifications

https://github.com/manaflow-ai/cmux
121•lawrencechen•9h ago•58 comments

My 1981 adventure game is now a multimedia extravaganza

https://technologizer.com/home/2026/02/16/arctic-adventure-2026/
94•vontzy•3d ago•29 comments

Don't Trust the Salt: AI Summarization, Multilingual Safety, and LLM Guardrails

https://royapakzad.substack.com/p/multilingual-llm-evaluation-to-guardrails
202•benbreen•3d ago•80 comments

Measuring AI agent autonomy in practice

https://www.anthropic.com/research/measuring-agent-autonomy
97•jbredeche•17h ago•46 comments

Show HN: A physically-based GPU ray tracer written in Julia

https://makie.org/website/blogposts/raytracing/
177•simondanisch•20h ago•69 comments