Skip to content
0:00 / 0:00

#zig#pointers#slices

Solving Common Pointer Conundrums - Loris Cro

Zig SHOWTIME

17K subscribers • 45 videos on Hallway

YouTube
  • Recommended by 4 people: 4 in engineering communities.
  • A classic. People kept recommending it from 2020 to 2024.
  • Presented by Loris Cro, VP of Community

Intermediate. Assumes working knowledge.

16K views6 years ago4 recommendations

Programming languagesSystems programmingZigMemory management

Loris Cro explains the relationships and differences between pointers, slices, arrays, and strings in the Zig programming language.

From Zig SHOWTIME #13 Subscribe to the Zig SHOWTIME Newsletter! zig.show

Why it's here

4 people recommend this

What people say. Recommended as an excellent explanation of Zig's pointer types, slices, and memory layout for developers learning the language.

Moments people linked to

u/jackdbdin r/Zig • Recommends

On “What book can you recommend to learn zig?”

…For example: * bump allocator * bump allocator with expandable memory (aka arena allocator) * testing allocator (very useful when you are writing tests because it will scream at you when your code has memory leaks) Maybe write some tests that allocate some memory to see how to use them. And of course watch this excellent talk by Benjamin Feng: [video This talk about memory allocation by Jorge Rodriguez is also great: [video Also, you'll need to understand the different types of **pointers** in Zig, so have a look at Solving Common Pointer Conundrums by Loris Cro: [video As for some materials on stack, heap, etc, try googling something like "memory layout of a C program". I woul…

u/kristoff-itin r/Zig • Recommends

On “Returning struct from function break data when allocating”

…to allocate that array on the heap or transform Meshdata to a struct that carries an array, instead of a slice. The rule of thumb is: if it's static length go for an array, otherwise go for the allocator and leave Meshdata unchanged. If you're not familiar with slice vs array and want more examples of how to circumvent the local variable problem: link to this video…

u/kristoff-itVP of Communityin r/Zig • Recommends

On “How to convert numerical values to string? And vice-versa?”

Ah, forgot to answer about the inverse operation: use parseInt. https://github.com/ziglang/zig/blob/905c85be968e6823305887474d2a821dac82f16c/lib/std/fmt.zig#L1259 If you need more information about the relationship between arrays, pointers, slices and strings, take a look at this: link to this video

u/jshhollandin r/Zig • Recommends

On “[]const u8 and noob questions on strings”

A great video reference for this sort of thing is Loris Cro's showtime lecture here: link to this video He explains all the different kinds of pointers and slices available in Zig - it's a good way to have everything explained in one place in one voice.

u/buck-birdin r/Zig • Recommends

On “expected type '[*:0]const u8', found '[]const u8'”

…arning Zig a couple months ago. Going through this is just part of the process until there are more books out there. Even though the question was answered already with `file.name0.. :0`, hoping to help give you some more insight as to what's going on... First, you need to spend an hour watching this video. It'll help explain pointer types. [video Second, `*:0const u8` is just Zig's way of saying a null terminated string, aka a C-string. However, Zig doesn't use strings in the same way. That's technically considered a many item pointer in Zig. Which means an unknown length of `u8s` (aka, chars). A Zig "string" is totally different (in a good way). It works more like Pascal did i…

u/kristoff-itVP of Communityin r/Zig • Recommends

On “expected type '[]const u8', found 'u8'”

If you want an overview of pointer types in Zig and how they relate to array, optionals ant strings, take a look at link to this video :)

u/jshhollandin r/Zig • Mentions

On “How to set strings into "*const []u8"?”

`*const []u8` is rarely a useful type, since slices are pointers already. As the compiler told you, the string literal `"sample"` has type `*const 6:0u8`, a const single-item pointer to a null-terminated array of 6 `u8`s. The "normal" string type in Zig is `[]const u8`, which the string literal `*const n:0u8` will coerce into. So changing `*const []u8` into `[]const u8` in your sample code should get it to compile. I'll once again link /u/kristoff-it's excellent "Solving Common Pointer Conundrums" video as it's a great way to get your head around Zig's various pointer types.

u/voroskoiain r/Zig • Mentions

Convert []const u8 to []u8 I have a program where I have included a file with embedFile. I create some tokens with std.mem.tokenize() and start to iterate over them: while (it.next()) |line| { var foo: []u8 = line; } This part fails, as line is a \\const u8. I have watched a video from Loris Cro ([video where he states that You should not use \\u8 when You do not want to modify the slice, but I do want to modify it (want to call std.sort.sort() later on). How can I convert these types? I just can not figure it out. Edit: Meanwhile I have found this: https://www.reddit.com/r/Zig/comments/jwdm50/comment/gcpn447/?utm\_source=share&utm\_medium=web2x&context=3 So I unde…

u/AliensAbductedDittoin r/Zig • Mentions

On “Sooooo.... second n00b question.”

This is a good video to watch about Zig's pointers :) link to this video

u/kristoff-itVP of Communityin r/Zig • Mentions

On “How does one convert a string literal into []u8?”

A string literal is a pointer to an array of bytes, which does automatically coerce to a slice, but string literals are immutable so they can only become `[]const u8`. The `const` part is important in this case. If you want I have a talk dedicated to this precise class of questions: link to this video

u/Airtunein r/Zig • Mentions

On “Using Zig to Call C Code: Strings”

[video 22:24 - 24:10 The \`\*const\` type appears to be a special case for string literals that is different from const variables.
1View on Reddit

u/Extension-Ad7241in r/Zig • Mentions

On “Error in "pub fn init(buffer: []u8) FixedBufferAllocator" signature?”

Thank you both for informing me; Obviously a total noob issue. I tried to search but it's difficult because I'm not even sure what the right question is sometimes! I use the standard documentation, and search for things before I ask, and that's how I just found https://ziglearn.org/chapter-1/ & [video on another Reddit post, and some other information but: Would you have any additional resources that you found helpful for this specific issue and Zig in general?