Questions tagged [zig]

Zig is an open-source programming language designed for robustness, optimality, and clarity.

Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

Website:

Documentation:

Zig has many features for low-level programming, notably: packed structs (structs without padding between fields), arbitrary width integers and multiple pointer types. Zig has no hidden control flow, no hidden memory alocation, no macros (but therefor compile time code execution)

212 questions
1
vote
1 answer

std.json.parse and memory management

I have the following zig code (simplified): const allocator = ...; const Entry = struct { name: []u8, }; fn list() ![]u8 { var entries = try std.json.parse([]Entry, ...); defer std.json.parseFree([]Entry, entries, ...); return…
Laney
  • 1,571
  • 9
  • 7
1
vote
1 answer

zig project with multiple exes and libs

I want to setup a zig project that builds several executables and static libs. This is the structure I have in mind: build.zig scr/ exe0/ depends on lib0, lib1 exe1/ depends on lib1 lib0/ lib1/ I think one way to do…
B_old
  • 1,141
  • 3
  • 12
  • 26
1
vote
1 answer

How to create a type '[*c]const [*c]const u8' for paramValues of PQexecParams

I'm trying to use the libpq library in zig. I'm trying to pass paramValues to PQexecParams. I'm just not sure how to create the required type. The type required by the documentation is: const char * const *paramValues So something like: const char…
pooya72
  • 1,003
  • 9
  • 15
1
vote
1 answer

Can't use .len of a bidimensional array

I have this simple code that doesn't compile. const s = [_][_]int { [_]int{08, 02, 22, 97, 38, 15, 00}, [_]int{49, 49, 99, 40, 17, 81, 18}, [_]int{81, 49, 31, 73, 55, 79, 14}, [_]int{52, 70, 95, 23, 04, 60, 11}, [_]int{22, 31,…
tuket
  • 3,232
  • 1
  • 26
  • 41
1
vote
1 answer

I get Segmentation fault when using X11 C library with Zig

I'm trying to use X11 with Zig. Using the code from https://rosettacode.org/wiki/Window_creation/X11#Xlib as an example, I came up with this minimal example: const c = @cImport({ @cInclude("X11/Xlib.h"); }); pub fn main() void { var…
1
vote
1 answer

zig creating a C library

Closed - see the end of the entry I want to make a C callable library using the zig language. I decided to start with the two examples in the Zig documentation. "Exporting a C Library" and "Mixing Object Files". In each case I copied the three…
roblackwell
  • 51
  • 1
  • 6
1
vote
1 answer

Does the Zig compiler consider arrays with comptime variable lengths as possible zero length arrays?

I'm experimenting with n-dimensional arrays in Zig. const expectEqual = std.testing.expectEqual; fn NdArray(comptime n: comptime_int, comptime shape: [n]comptime_int) type { if (shape.len == 0) { // zero dimensional array, return the…
1
vote
1 answer

What is the workflow for implementing ERTS NIFs in Zig?

What is the workflow for implementing ERTS NIFs in Zig? Is there something comparable to Rustler for NIFs authored in Rust?
Max Heiber
  • 14,346
  • 12
  • 59
  • 97
1
vote
1 answer

Set core affinity for thread in zig

What is the suggested way to set core affinity for thread in zig programming language? Can't find anything similar in the docs…
1
vote
1 answer

Expected type ?extern fn

I don't know any C or Zig. But I'm trying some stuff out and I'm really impressed so far. I'm trying to run a web server in Zig using a C library "lwan". It works. But I'm having some trouble with making my handler…
CTXz
  • 11
  • 2
1
vote
2 answers

How can I generate a 256 bit mask

I have an array of uint64_t[4], and I need to generate a mask, such that the array, if it were a 256-bit integer, equals (1 << w) - 1, where w goes from 1 to 256. The best thing I have come up with is branchless, but it takes MANY instructions. It…
Shawn Landden
  • 73
  • 1
  • 8
0
votes
2 answers

How do I compare two zero-terminated but different length strings in Zig?

For example where an external C library is returning a fixed-size buffer containing a null-terminated string. What would be the the equivalent of strcmp() to compare these two buffers? std.mem.eql fails because they are different sizes. var string1:…
snowcrash09
  • 4,694
  • 27
  • 45
0
votes
1 answer

function pointers must be single pointers

Trying to build a simple router, where a given function is called based on the required route, so for testing I started with the below: // routes.zig const std = @import("std"); //pub const LoadFunction = fn (i32) f32; pub fn init(routeRegister:…
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
0
votes
1 answer

JavaScrip file is not loaded correctly

I'm trying to write a simple server that is loaded static files, so i wrote the below: const std = @import("std"); const http = std.http; const log = std.log.scoped(.server); const server_addr = "127.0.0.1"; const server_port = 8000; // Run the…
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
0
votes
1 answer

Memory leackage in reading the file content

I've the below code for reading and printing the content of the specified file, this code is working fine: const std = @import("std"); pub fn main() !void { const contents = try readFile("index.html"); std.debug.print("File contents:…
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203