Segfault on C radix sorting algorithm | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Segfault on C radix sorting algorithm

I keep getting a segmentation fault (timeout?) error when compiling this code in C. Can anyone help me find where is the error? I don't think I'm accessing any forbidden memory addresses, but who knows. https://code.sololearn.com/ceWZEBf2Rmj2/?ref=app

7th Apr 2020, 8:19 AM
Lui Martinez Laskowski
Lui Martinez Laskowski - avatar
2 Answers
+ 1
In loop, count[arr[i]%(pos*10)]++; This lead to index out of range, You declared count size as 10 maximum. You have array values greater than 10 So when greater than 10, for example 634, if pos =10, Then, 634%100=34 count[34] is array index out of bounds so raise segmentation error...
7th Apr 2020, 11:20 AM
Jayakrishna 🇮🇳
0
thanks, it's fixed! I'd forgotten to deal with the characters after the current digit so was getting multiple digit numbers. a simple division took care of that.
7th Apr 2020, 5:20 PM
Lui Martinez Laskowski
Lui Martinez Laskowski - avatar