This video introduces a C compiler that compiles C code to WebAssembly, leveraging WebAssembly's built-in garbage collection to provide automatic memory management for heap-allocated data structures. Unlike traditional C where developers manually manage memory using malloc and free, this approach uses WebAssembly's GC types (such as structs, arrays, and anyref) that exist in a separate managed heap outside linear memory. The compiler supports declaring GC structs with double underscore prefix, allocating with new, and accessing fields with arrow notation. Arrays can be declared with array types and allocated with array new. The anyref type serves as a supertype for all GC reference types, enabling generic containers similar to Java's ArrayList. This approach eliminates memory leaks and dangling pointers while maintaining compatibility with standard C syntax.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
C with Garbage CollectionAdded:
Uh, hello, internet. So, uh, C is a language where you manage your own memory, right? malloc, free, dangling pointers, memory leaks, that's the deal. You get control, you take responsibility. But, what if the your compiler could offer you a second option, right? Heap-allocated data structures managed automatically, no freeing required.
Uh, this is my C compiler. It compiles C to WebAssembly, and WebAssembly now has garbage collection. It's why I added it to C.
Basic idea is this.
A normal C program only uses linear memory, a a flat black block of bytes. You allocate with malloc, free with free, and use pointers to navigate that block.
Wasm GC types live somewhere else entirely. They're on the man- engine's managed heap outside linear memory. You can't take their address, you can't embed them inside a normal normal C struct. They exist in a separate universe.
It sounds respec- restrictive, and it is, but the trade-off is that you never you never free them. The engine handles it.
So, let me let me show you what this looks like in practice.
You declare a struct with uh double underscore struct. You allocate with new.
Fields start zero-initialized, and you set them with arrow assignment. Just like a pointer to a struct in regular C.
And that's intentional. A GC reference is conceptually a pointer. It refers to a heap object. The star and the arrow make that explicit. C23 introduces the auto keyword, which lets the compiler infer the variable's type. Uh, and together with a typedef, you can clean clean this up quite a bit. So, yeah, it looks uh it looks nice.
Okay, so GC arrays.
Uh, declare you declare the type with array int.
Uh, you allocate with array new.
And then you get the length with array len. You can access the elements using the the usual subscript syntax.
Uh, you can also create arrays with array literals like this, literal values like this.
And this compiles down to the array.new fixed uh opcode.
Okay. So far our containers have have had a fixed element type, right? But sometimes you want one that holds anything like what uh like object in Java or or sort of like void star in in C, right? So WasmGC gives you a few choices of types for that, but but for us we're using anyref.
So it's a supertype of every GC reference type we have, uh like structs and arrays, and box primitives.
Uh and you you can also unbox through casting as well.
So this turns out to be enough to build some really cool like uh generic containers that you you usually take for granted in other languages that are just hard to have in C. So here's the heart of a Java-style array list here.
And yeah, so so this is how you would use it. Um I think looks pretty nice. Uh and yeah, the full source is in the compiler repo.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 viewsβ’2026-05-28
How agent o11y differs from traditional o11y β Phil Hetzel, Braintrust
aiDotEngineer
450 viewsβ’2026-05-28
Re: π£οΈπthepropheduπ2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 viewsβ’2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanationπ―β
LearnwithSahera
1K viewsβ’2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 viewsβ’2026-05-29
Search Algorithms Explained in 60 Seconds! π€π¨
samarthtuliofficial
218 viewsβ’2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 viewsβ’2026-05-30
Instagram accounts got PWNed
EricParker
13K viewsβ’2026-06-03











