Blog Post: Problems of C, and how Zig addresses them

Hi! I’ve written a blog post to show how Zig solves C problems:

avestura.dev/blog/problems-of-c-and-how-zig-addresses-them

5 Likes

In Zig it just works:

fn foo(arr: [100]i32) void { // pass array by value
   
}

fn foo(arr: *[100]i32) void { // pass array by reference
   
}

As far as I know, in Zig the “pass by value” syntax only means that parameter is non-mutable, but the compiler may choose to pass by reference or not, for pretty much any type where the compiler thinks it might be faster.

2 Likes

@TUSF That’s true. But that’s still “pass by value” to the programmer. It’s just that in Zig parameters are constant. Due to this fact the actual copying can be eliminated in many cases, which is an implementation detail.
You can still do

fn foo(_arr: [10]i32) void {
   var arr = _arr;
   // mutate arr here
}
1 Like

Thanks for sharing, that was a good overview. The only thing that feels missing is a section dedicated to defer and errdefer, which solve a huge C painpoint.

Thanks for pointing this out.

I would personally prefer if this would be more explicit, i.e. if I want tge pointer to be const, I need to qualify it as const. Otherwise it feels like magic and a little bit like hidden control flow.