Get the latest tech news

Zig's Memcpy, CopyForwards and CopyBackwards


Why we have 3 similar function and how aliasing impacts the compiler

More generally, aliasing means that compilers need to handle the possibility that writing to a pointer affects other variables. Still, unless you're copying into newly allocated memory, it's probably worth spending a few seconds to consider whether the source and destination could overlap and, if so, whether std.mem.copyForwards(or, less likely in my experience std.mem.copyBackwars) is the correct choice. Some guidelines use the word "avoid", but I've settled on merely being more mindful of it (at least for now); maybe now and again I manage to limit the scope where two variables reference the same memory.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of zig

zig

Photo of memcpy

memcpy

Photo of CopyForwards

CopyForwards

Related news:

News photo

Basic MetaProgramming in Zig

News photo

Spice: Fine-grained parallelism with sub-nanosecond overhead in Zig

News photo

C Macro Reflection in Zig