#Help with segmentation fault
1 messages · Page 1 of 1 (latest)
the reason is because slices have runtime length so you can't inline for loop over them. inline for unrolls the loop at comptime which isn't possible for a runtime known
I am using 0.14.0-dev.2591+5333d2443
Thanks for the reply btw
interesting. i'm on 0.14.0-dev.2546+0ff0bdb4a and see that error message.
i think your comment about 'it compiles fine' might be because zig does lazy analysis. so if you don't call it, it doesn't get analyzed and some compile errors won't be discovered.
were you actually using it?
I just looped with indexes and it worked
but anyway, i couldn't undestand why it wasnt working, but i get it now
exactly
also @as(x, usize) isn't correct
changed this now to :
for (0..(N + 1)) |i| {
for (0..(arr.len)) |j| {
if (j <= i) {
dp[i] = @min(dp[i], 1 + dp[i - j]);
}
}
}
ok good. if you wanted to unroll it you might declare arr a comptime param like this
fn min_cost(comptime arr: []const i32 ...
now it works fine so i am going to leave it as is. Was a skill issue, writting C++ since i was very young influenced me a lot i guess 😄
i understand the comptime as a parameter but i want it to be as simple as possible, it's for competitive programming usage
Thanks guys
for sure. does c++ have inline loops? i can see how that might be a little different.
No we only have inline functions
i think constexpr is similiar to comptime
but inline in zig is very important. In C++ i almost never use it.
```ts
for (arr[0..i]) |x| {
dp[i] = @min(dp[i], 1 + dp[i - @as(usize, @intCast(x))]);
}
```
edited. not sure if its correct but that seems like what you intended
and may be more performant that the if, idk
probably you forget to use the elements of arr
from your code i guess the elements of arr should be unsigned