#Make a slice between two pointers

1 messages · Page 1 of 1 (latest)

boreal snow
#

I have two pointers, first one points to the stack base, the other points to the topmost element on the stack. I want to make a slice []u64 so that I can access the stack elements?

Tried:

const p: *Value = @ptrFromInt(@as(usize, @intCast(base)));
const len: usize = @intCast(end - base);
const s: []Value = p[0..len];

But this seems to only work at comptime.

stuck storm
#

a) you cannot slice a *T, you need a [*]T
b) end - base is a length in bytes, the length used in slicing is a number of elements, so you likely need (end - base) / @sizeOf(Value)

boreal snow
#

thanks @stuck storm , it worked 😄

elder elm
#

also should definitely assert that the pointer has an alignment of 8 if you want to re-interpret it as a slice of u64s

boreal snow
#

how do I do that?

stuck storm
elder elm
#

Oh cool

boreal snow
#

hm, how do I mark it as answered thonk

elder elm
#

reply to your first message with a check mark

#

*react