#Help with segmentation fault

1 messages · Page 1 of 1 (latest)

slate zodiac
#

i guess that code doesn't compile and segfaults the compiler? what version of zig are you using? when i tried to compile it on 0.14-dev i see

/tmp/tmp.zig:13:21: error: unable to resolve comptime value
        inline for (arr) |x| {
                    ^~~
#

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

sour sonnet
#

I am using 0.14.0-dev.2591+5333d2443
Thanks for the reply btw

slate zodiac
#

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?

sour sonnet
#

I just looped with indexes and it worked

#

but anyway, i couldn't undestand why it wasnt working, but i get it now

remote viper
#

also @as(x, usize) isn't correct

sour sonnet
slate zodiac
#

ok good. if you wanted to unroll it you might declare arr a comptime param like this

fn min_cost(comptime arr: []const i32 ...
sour sonnet
#

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

slate zodiac
#

for sure. does c++ have inline loops? i can see how that might be a little different.

sour sonnet
#

No we only have inline functions

remote viper
#

i think constexpr is similiar to comptime

sour sonnet
#

but inline in zig is very important. In C++ i almost never use it.

slate zodiac
#
```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

remote viper
#

from your code i guess the elements of arr should be unsigned