# Segfault calling C function, any advice?

**URL:** <https://discourse.julialang.org/t/segfault-calling-c-function-any-advice/94730>\
**Category:** General Usage\
**Tags:** segfault\
**Created:** [February 16, 2023, 4:35pm UTC](https://discourse.julialang.org/t/segfault-calling-c-function-any-advice/94730 "2023-02-16T16:35:07Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 16, 2023, 11:34pm UTC](https://discourse.julialang.org/t/segfault-calling-c-function-any-advice/94730/8 "2023-02-16T23:34:06Z")

</div>

> [@CodeGodz](#):
>
> `k = length(Set(in_vector))`

I think it should be:

```julia
k = maximum(in_vector)+1

```

As the parameter is the alphabet size (which includes missing letters). So essentially, the maximum value in the input array.  
Specifically, if input is `[1,5,2,2]` is should be at least `5+1` = `6` and not `3` which is the `length(Set([1,5,2,2]))`. The `+1` on the maximum is because `0` is a an alphabet value too.

As, the maximum value in the example is rather big, it might be productive to first reduce the maximum alphabet value with a lookup table as follows:

```julia
v = Int32[2,1,1,1,1458521]
d = Dict(Iterators.map(reverse,pairs(sort(unique(v)))))
newv = Int32.(get.(Ref(d), v, 0))

```

Now, the `maximum(newv)+1` should be minimal for the input vector, and ready to be shipped to `libsais_int`. Since the ordering of inputs are preserved, the output suffix array is also identical.

---

_[View the full topic](https://discourse.julialang.org/t/segfault-calling-c-function-any-advice/94730)._
