451 questions
0
votes
0
answers
96
views
Why does my Zig VGA print function produce corrupted ASCII when iterating over string?
While writing a VGA driver for my zig kernel I came across a very weird problem. When called, my print function displays corrupted ascii characters to the VGA display (not the chars corresponding to ...
-1
votes
1
answer
92
views
Is there a way to capture a value from the left expression and use it on the right expression?
I want to modify or compute a value before assigning it to a const. Is there a way to do this in a single line expression?
I can use labeled block syntax, but I’m wondering if there’s another syntax ...
-1
votes
1
answer
116
views
How to create a dictionary in Zig? [closed]
Is there a Zig equivalent of a C# Dictionary or a Java Map or a Go map that stores key/value pairs in a structured way? If so, how do I create one?
-1
votes
1
answer
161
views
Weird behavior when using an image as both a color and input attachment
I am trying to implement color blending in the fragment shader because I want to use the alpha channel as a sort of dynamic stencil buffer for future draw calls.
I have attached the swapchain image ...
7
votes
1
answer
276
views
Using zig to compile C to web assembly
I believe that it should be possible to use zig to compile a simple c library to web assembly.
I am using the following c code
// add.c
int add (int first, int second)
{
return first + second;
}
...
0
votes
1
answer
111
views
How to add 2 would be overflowing integers in zig and store the result in a larger variable type
Trying to add to smaller values and save them into a larger variable type with this code:
const a: u4 = 0b1101;
const b: u4 = 0b0101;
const c: u5 = @intCast(a) + @intCast(b);
...does not ...
0
votes
1
answer
113
views
How do I append a timestamp data type with zuckdb/duckdb appender in zig
I need to append candlestick bars to Duckdb that have a timestamp data type. Since I don't know how I have used varchar for time instead. How do I do it properly so that I can query based on timestamp?...
5
votes
1
answer
248
views
Linking Zig and Go for windows API
I'm trying to link a zig static library with golang. here is my build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const windows = b.option(bool, "windows&...
3
votes
1
answer
298
views
How can I iterate over struct fields at compile time in Zig to generate a serializer?
I'm learning Zig and I'm trying to write a generic function that automatically serializes any struct into JSON. In languages like Rust or Go, there are libraries that can iterate over the fields of a ...
1
vote
1
answer
89
views
Double fault when i enable interrupts (via sti)
im making an os kernel in zig (grub to boot) and im trying to get interrupts to work specifically hardware interrupts and as soon as i enable them (i have a pic, gdt and idt setup) it gives me a ...
2
votes
1
answer
149
views
Weird problems with Zig Array List(s)
In the below code, I have the function "get_specific_files". The objective of this function is to check if certain filenames contain some word, and then return all the files that contain ...
2
votes
1
answer
99
views
Why does UEFI GetMemoryMap return EFI_BUFFER_TOO_SMALL after allocating a buffer of the reported size?
I'm writing a UEFI application in Zig.
I'm calling GetMemoryMap() twice: once to get the required buffer size, then again after allocating the buffer using AllocatePool().
Here's my code:
fn ...
0
votes
1
answer
85
views
Printing variable in function in external module results in a different output than printing the variable from main
I am trying to make a sort of input() function for a project I've been working on.
The function simply reads from stdin and returns the string.
Here's what I came up with:
const std = @import("...
0
votes
0
answers
176
views
How to create an efficient implementation for multiarraylist-like type for unions in zig?
I know MultiArrayList supports tagged union, but it does it by converting it into struct containing untagged union and a tag field, effectively creating 2 ArrayList fields, one for union and the other ...
0
votes
0
answers
140
views
Does the zig compiler support symbol wrapping, esp. for glibc?
Does zig support compiler/linker functionality similar to that of gcc when using the -Wl,--wrap option?
I am new to zig and admit I'm probably just doing it wrong.
For instance I have a C only project ...
1
vote
1
answer
90
views
Seg fault when trying to use allocator.create to create a pointer of a union type using arena.allocator in Zig
On a pet project of mine, I'm trying to create a pointer to an expression using the arena allocator along with the heap page allocator, but I'm encountering some issues.
The expression is defined as a ...
0
votes
1
answer
73
views
error: no field named 'parseIdentifier' in struct 'parser.Parser' when trying to store functions in hash in Zig
I'm working on a new pet project and i came across this error when trying to store functions in a hash in zig.
the error happens on the try p.nud_handlers.put(Tokens.IDENT, p.parseIdentifier); line
...
2
votes
1
answer
178
views
How to generate an executable output file for "zig build test"
It seems that "zig build test" will run the unit tests automatically, but I want to generate an executable file which calls all the unit test functions, then I would like to load the ...
0
votes
2
answers
163
views
How to avoid unsigned values error in zig?
I have the following code
const std = @import("std");
const stdio = @cImport({
@cInclude("stdio.h");
});
const ioctl = @cImport({
@cInclude("sys/ioctl.h");
@...
3
votes
1
answer
296
views
How can I change std.log log level from the command line in Zig?
I'm using std.log in Zig and I want to change the log level (e.g. to .debug or .info) when running tests using the command line.
Is there a way to set the log level from zig build, for example like:
...
0
votes
1
answer
496
views
How can I generate a timestamp string using zig?
using zig, I need to generate a timestamp that format exactly or almost matches 2025-04-18T14:03:52.000Z. How can I go about it? using the standard library only
2
votes
1
answer
285
views
Log messages not showing when running tests in Zig (zig build test --summary all
I'm trying to use Zig's logging functionality within my tests, but when I run the tests using the command zig build test --summary all, I don't see the log messages I expect.
What I've tried:
I've set ...
0
votes
1
answer
464
views
How do I correctly use Optional with pointers in Zig to avoid const errors?
During the study and practice of optionals and pointers in Zig, I encountered behavior that was quite unclear to me. I can't quite understand what exactly the compiler is trying to tell me.
In the ...
0
votes
1
answer
134
views
Initialising a nullable array struct field in zig 0.14.0
I'm using zig 0.14.0, and having issues with setting a nullable array. I've come up with an example (this is not my main code base or anything close to what I'm doing) to illustrate the issue.
const ...
3
votes
1
answer
311
views
Zig as C Linux->Mac cross-compiler for go project with go-sqlite3 error: unable to find dynamic system library 'resolv'
I'm building a Go application with native (CGO_ENABLED=1) SQLite support using https://github.com/mattn/go-sqlite3.
I'm trying to get the builder docker image to a reasonable size. (goreleaser/...
1
vote
1
answer
147
views
How to run a smaple xcb example in zig programming language? [closed]
I am trying to write a simple window manager with xcb in zig where there is an event loop with some possible events (key press, ...). My system is running an arch linux and all the necessary libraries ...
0
votes
0
answers
130
views
How can I include xcb.zig and glfw into my zig project using zig build system with no error?
I am completely new to zig and system programming. I do not understand anything from the zig build system and I want to include glfw and xcb.zig in my project. I have been looking for documentations ...
1
vote
1
answer
132
views
How to cast a runtime slice range to fixed array in zig 0.14.0?
Without altering func()'s signature, How am I able to do something like this?
const std = @import("std");
fn runtimeNumber() u16 {
return 32;
}
fn func(b: [4]u8) void {
std.debug....
1
vote
0
answers
108
views
Inject jmp to my function at address of symbol declaration
I want to place jump to my function on address of other function declaration, specifically CreatePen function for gdi. I'm using zig and that's what i've made so far:
const windows = @import("std&...
3
votes
1
answer
251
views
JS/C Interop with zig cc and wasm
I am writing a webassembly demo in C to match a similar demo I wrote in zig.
I am currently able to call C functions from JS, and interact with shared memory on either side. However, I can't seem to ...
0
votes
1
answer
127
views
Should a function return []const u8 or []u8 if the caller owns the slice
I noticed that a library I'm using frequently returns []const u8 instead of []u8 when it allocates memory and the caller is responsible for freeing it.
Should a function return a mutable slice []u8 ...
1
vote
1
answer
247
views
How to set RISC-V `-march` for zig build
When cross-compiling for RISC-V with Zig I believe you do something like this:
pub fn build(b: *std.Build) void {
const target = b.resolveTargetQuery(.{
.cpu_arch = .riscv32,
.abi =...
1
vote
1
answer
283
views
Why is copying more efficient in Zig remap?
The remap function in Zig's memory allocator interface is supposed to "attempt to expand or shrink memory, allowing relocation". In particular, it mentions that...
A null return value ...
1
vote
1
answer
378
views
Zig unable to evaluate comptime expression
I'm trying to learn Zig using the website https://exercism.org/. I was solving this exercise, which requires us to find the nth prime number.
My idea was to generate all of them during comptime and ...
2
votes
1
answer
133
views
Zig expected type '[]usize', found 'type'
I'm trying to learn Zig using the website https://exercism.org/. I was solving this exercise, which requires us to find the nth prime number.
My idea was to generate all of them during comptime and ...
0
votes
2
answers
103
views
Zig(`sytax/declaration`) Semantic of error as only return type of function. Or what actually do `catch`
I don't know if this is correct behavior according to the author, or if there's a mistake in compilation.
const std = @import("std");
const expect = std.testing.expect;
const expectError = ...
2
votes
1
answer
187
views
How to override Zig Function Return (e.g. !u32 vs u32) Compile Behavior?
I'm new to Zig and have been going through some premade exercises to get a hang of it. I got stuck on one because I misunderstood what the compiler was trying to tell me.
I've mocked up a sample of ...
6
votes
1
answer
785
views
Constant struct fields in Zig
Dabbling with Zig and have a question about const fields in structs. In the simple struct below I have started to implement a simple matrix data structure. Presumably once I have instantiated a ...
1
vote
1
answer
559
views
Global and static local variables initialization
I'm trying to learn Zig v0.13.0, and I'm playing around with small programs. While playing, I tried to create a "global allocator", which is more convenient for now.
So at first, I did ...
4
votes
1
answer
165
views
Deinit global and static local variable
I'm having trouble with memory management in Zig (using v0.13.0), regarding global and static local variables deinitialization.
I've written the following program, but memory leaks, and thus the ...
1
vote
1
answer
324
views
Translate C enum into Zig namespaced enum
Given the following C code:
// h.h
typedef enum E {
A,
B,
C
} E;
Zig translates this to
pub const A: c_int = 0;
pub const B: c_int = 1;
pub const C: c_int = 2;
pub const enum_E = c_uint;
pub ...
1
vote
1
answer
816
views
Parsing array of json objects in zig
I am attempting to parse a .json file in zig however am very new to zig and having no luck finding much documentation for this. Here is my working example of code:
const std = @import("std");...
-2
votes
1
answer
640
views
How to idiomatically return an accumulated slice/array from a Zig function
See the following code
const std = @import("std");
const print = std.debug.print;
test "Demo accumulation" {
const accumulated = try accumulate();
print("DEBUG - ...
0
votes
1
answer
1k
views
How to create a Struct with an array-typed field in Zig?
As part of testing my solution to Advent Of Code 2024 day 5, I'm trying to create a struct containing an array created from an literal, but I'm getting an error that I can't resolve:
const std = @...
0
votes
2
answers
1k
views
How can I write a Zig function that can accept and return strings?
I am very new to Zig (working through Advent Of Code in it), and I am very confused by its handling of strings (or, I should say, []u8s) as function arguments and return types.
TL;DR what is the ...
1
vote
0
answers
120
views
Notify icon window won't exit program if hwnd set as foreground window
I have a notification icon app in zig using Win32 API. It has this code as its WndProc:
const std = @import("std");
const windows = std.os.windows;
const zeroes = std.mem.zeroes;
pub const ...
0
votes
1
answer
220
views
error: type capture contains reference to comptime var
I'm new to Zig and experimenting with the type system. I have a piece of code that does some comptime operations to determine the shape of a tensor. I'd like to use this comptime variable for some ...
1
vote
1
answer
49
views
Problem reading wav header in zig, finding irrelevant values
Im trying to read the header of a wav file ( wav file that works with media players) in my program,
but i only find invalid values ( for sample rate, number of channels ect)
pub const WavObject = ...
1
vote
1
answer
416
views
How to determine types of Reader and Writer instances in Zig?
I am trying to produce a struct that holds references to each of the readers and writers of stdin/stdout/stderr
I am stuck at trying to declare the appropriate types however. My simplest code:
const ...
2
votes
1
answer
229
views
Zig cross-compiling riscv64-linux-musl fails on Windows
I'm using a simple github actions to try to build some ELF binaries using windows-latest just to check if it works: https://github.com/libriscv/godot-sandbox-programs/blob/windows/.github/workflows/...