5 ms·
I discovered Zig a few days ago. I'm evaluating it for use with my STM32 microcontroller. A few observations: * it cross-compiles to other architectures withou
by blippage 5y ago
I discovered Zig a few days ago. I'm evaluating it for use with my STM32 microcontroller.
A few observations:
* it cross-compiles to other architectures without any extra libraries required. The chances are that you've already installed the GNU cross-compiler tools, but it's a nice touch to have an all-in-one solution.
* you can split memory into bitfields. This is useful for microcontrollers, where different parts of a 32-bit register do different things. The compiler can check the field widths, too. C++ does have bitfields, but AFAIK, it doesn't give guarantees as to exactly how the bits might be packed.
* I can have a single registers file, that lays out the memory map of the microcontroller, somewhere in my path. In C, vendor-supplied libraries tend to have definitions all over the place, so a single file is a bonus.
* a module system. You write the build script in Zig itself.
* no macros, but it can perform compile-time execution. I've been wondering if compile-time execution actually provides a whole new way of thinking about programming.
* async/await/suspend/resume. Ooo, a way of doing cooperative programming baked into the language itself. Seems very handy for microcontrollers
* a library system that is part of the language. This means that you could write bare-metal systems, and it will give you things like maps and list. You don't have to try to integrate third-party libraries like newlib. Presumably it doesn't all work out of the box, though. I've not delved into the mechanics of it yet.
There are plenty of other things of well, but those are the ones that jumped out at me if you're interested in system-level work.
Zig is squarely aimed at C, a kind of "C done right". The syntax is fairly intuitive to understand. In terms of size, there's virtually a one-to-one translation between C and Zig. Zig aims for correctness and transparency, although it isn't as strict as Rust.
To give perhaps one example, C99 introduced flexible array members, so you could do stuff like
```
void func(int n)
{
int arr[n];
}
```
And the question is: where/how is the memory allocated for arr? The answer is: probably on the heap. If you're using a microcontroller, though, heap allocation might be a big no-no. You need a memory allocator for starters, and you have to worry about memory fragmentation on resource-constrained systems.
Zig takes the philosophy, if I can put words in the developers mouths, of having a strong dislike of "stuff going on under the hood" that you don't know about. They take and ability to reason about what the code does seriously.
- hawski 5y agoIsn't your example code a stack allocated array? Flexible array members would rather be: struct Foo { int len; int data[]; } *foo = malloc(sizeof(struct Foo) + n*sizeof(foo->data[0])); foo->len=n;
- nrclark 5y agoZig is a really exciting language to see grow. As a minor correction to your post: C99's flexible arrays are stack-allocated, not heap-allocated. Since stacks are often small, this feature actually wound up causing a bunch of designers to hit stack overflows. It was downgraded to an optional extension in C11.