Pointer aliasing and reading/writing from/to overlapping memory, is this safe?

I don’t see any problem.


The source of the library function std.mem.copyForwards is:

pub fn copyForwards(comptime T: type, dest: []T, source: []const T) void {
    for (dest[0..source.len], source) |*d, s| {
        d.* = s;
    }
}

The only stated precondition is:

If the slices overlap, dest.ptr must be <= src.ptr.

You have the same way of copying with copyForwards and the precondition always holds (even for the last one, s[2].ptr < m[2].ptr).

2 Likes