c++ - ERROR: Segmentation Fault (Core dumped) Arrays? -
every time run program receive message: segmentation fault (core dumped). tried doing bit of research , seems issue has allocating illegal memory. i've tried debugging program , seems problem lies in linearsort() function since after commenting out, rest of statements work correctly.
#include <iostream> using namespace std; int main() { void linearsort(int [], int); int arr[10]; for( int j = 0; j < 10; j++) arr[j] = j +1; linearsort(arr,10); for(int = 0; < 10; i++) cout << arr[i] << " "; cout << endl; cin.get(); return 0; } void linearsort(int arr[],int n) { int temp; for(int pass = 0; pass < n - 1; n++) for(int cand = pass + 1; cand < n; cand++){ if(arr[pass] > arr[cand]){ temp = arr[pass]; arr[pass] = arr[cand]; arr[cand] = temp; } } }
for(int pass = 0; pass < n - 1; n++)
you incrementing wrong value, n++
should pass++
. meaning right now, you're accessing out of bound indices in array.
Comments
Post a Comment