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
3
votes
1 answer

How to create an empty slice of slices

How do I construct []const []const u8 without using an allocator? I can do var slice: []const []const u8 = undefined; slice.len = 0; // use slice But there surely must be a better way.
sigod
  • 3,514
  • 2
  • 21
  • 44
3
votes
1 answer

Using zig compiler as a library

Is there a way to use zig compiler as a library inside zig? After looking both in zig documentation, issues and on the internet, I can't find an answer to this question. In one of the issues it is said that this can be done at the current time, but…
trenta3
  • 123
  • 7
3
votes
1 answer

zig print float precision

In zig it is possible to print float values in decimal notation by using "{d}". This will automatically print the value at full precision. Is there way to specify the number of digits? Either for each value, or as some kind of global setting?
B_old
  • 1,141
  • 3
  • 12
  • 26
3
votes
1 answer

Simple log analysis with Zig

Motivated by https://benhoyt.com/writings/count-words/ , I have played a bit with rewriting an internal log analysis script in several languages (I will not go as far as in the article!). After Go (by myself) and Rust (with some help from SO), I am…
user15795022
  • 195
  • 1
  • 6
3
votes
2 answers

What happen when program reach unreachable on ReleaseFast? (Zig lang)

I read on Zig Doc it has undefined behavior. is that it? isn't there any way tho predict the behavior of the code after hitting unreachable? like if it's process next line or try to continue like unreachable never been there!
Sarvarian
  • 43
  • 5
3
votes
1 answer

How to create 2d arrays of containers in zig?

I'm trying to alloc 2d arrays of HashMap(u32, u1) in Zig: fn alloc2d(comptime t: type, m: u32, n: u32, allocator: *Allocator) callconv(.Inline) ![][]t { const array = try allocator.alloc([]t, m); for (array) |_, index| { array[index]…
lyhokia
  • 79
  • 5
3
votes
1 answer

How to avoid memory leaks when building a slice of slices using ArrayLists

I'm trying to build a slice of slices using multiple std.ArrayLists. The code below works, but the memory allocator std.testing.allocator warns me of a memory leaks wherever I append new elements to a sublist. const std = @import("std"); const mem =…
jackdbd
  • 4,583
  • 3
  • 26
  • 36
3
votes
1 answer

How do I print a UTF-16 string in Zig?

I've been trying to code a UTF-16 string structure, and although the standard library provides a unicode module, it doesn't seem to provide a way to print out a slice of u16. I've tried this: const std = @import("std"); const unicode =…
Sapphire_Brick
  • 1,560
  • 12
  • 26
3
votes
1 answer

How can I create multidimensional arrays of arbitrary sizes?

I am writing a function in Zig that should accept multidimensional arrays of arbitrary sizes. There can be limits but I am unable to hardcode the sizes in advance. Here is an example: const warn = @import("std").debug.warn; fn printMap(map: []const…
sdasdadas
  • 23,917
  • 20
  • 63
  • 148
3
votes
1 answer

Slice referring to out of scope data in zig language

The get function below looks to me like it returns a slice referring to data in an array that will be out of scope once the function returns, and therefore is in error. Assuming this is true, is there any way to detect this at compile time or even…
jcoder
  • 29,554
  • 19
  • 87
  • 130
3
votes
1 answer

Zig "translate c" doesn't translate main function

I created a C file: int main() { return 1; } I used Zig's translate-c command line option to generate a zig file, and I only get some global variable declarations like pub const __GCC_ATOMIC_TEST_AND_SET_TRUEVAL = 1; pub const __FLT16_MAX_EXP__ =…
ice1000
  • 6,406
  • 4
  • 39
  • 85
2
votes
1 answer

error: use of undeclared identifier 'c_char'

I'm trying to make game engine and game on this on zig (and C++), but I'm getting this following error: main.zig:75:54: error: use of undeclared identifier 'c_char' const zipArchive = zip.zip_open(std.mem.ptrCast(*c_char,…
2
votes
2 answers

Idiomatic way to free item memory in a zig ArrayList([] const u8)

I have an ArrayList([]const u8). I pass a pointer to it into functions that append to it. They append the results of calls to std.fmt.allocPrint(). To free everything up, the top-level function deinits the ArrayList after it frees all the items: var…
Bob Follek
  • 33
  • 5
2
votes
1 answer

Correct way to put pointers into an array?

I've been trying for a few days now to create structs and then organize their pointers into a 2D array, with no luck. I then tried to re-create my issue in a small test with a 1D array, and I did recreate it. Why cannot I not use an array of…
chugadie
  • 2,786
  • 1
  • 24
  • 33
2
votes
1 answer

How to do conditional compilation with Zig?

For example, I can add definitions for C/C++ preprocessor with CMake add_definitions(-DFOO -DBAR ...) and then I can use them for conditional compilation #ifdef FOO code ... #endif #ifdef BAR code ... #endif Is there a way to do the same…
1 2
3
14 15