Segmentation fault (core dumped)

Jan 18, 2017 at 1:32am
Hi guys, I'm learning algorithms and I made this simple selection sort algorithm. It compiles fine, but when I execute it, I get "Segmentation fault (core dumped)", why is that?
BTW, I know this isn't the best way to do it, but I tried to learn how to use the <vector> and it seemed really over-complicated, I couldn't learn how to create a simple list, like a did below, using <vector>. So if someone could shed a light on that to me, I'd be very grateful...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <stdio.h>

using namespace std;

void swapInArray(double v[],int indexA, int indexB){
	double temp = v[indexA];
	v[indexA]=v[indexB];
	v[indexB]=temp;
}

int indexOfMin(double v[],int startIndex){
	double minValue = v[startIndex];
	double minIndex = startIndex;
	for (int i=startIndex;i<10;i++){
		if (v[i]<minValue) {minValue=v[i]; minIndex=i;}
	}
}

void selectionSort(double v[]){
	for (int i=0;i<10;i++){
		swapInArray(v,i,indexOfMin(v,i));
	}
}

int main(){
	double v[] = {10.0, 9.3, 4.4, 9.1, 3.4, 11.0,43.0, 0.0, 1.0, 0.0};
	
	selectionSort(v);

	for (int i=0;i<10;i++) printf("%.2lf",v[i]);
		
	return 0;
}
Jan 18, 2017 at 1:48am
Hi, first step: compile with a high level of warnings. I used cpp.sh (the gear icon top right of the code), I turned on all 3 warning options

In function 'int indexOfMin(double*, int)': 
14:9: warning: variable 'minIndex' set but not used [-Wunused-but-set-variable] 
18:1: warning: no return statement in function returning non-void [-Wreturn-type]


Warnings are your friend, they tell you about potential problems in your code. Code may compile without many warning options, but will crash - as you have discovered. When I am coding I use as many warning options as I can, and am not finished until I have no warnings at all.

Line 10 assumes that there are 10 elements in the array. Specify the size of the array like this:

1
2
// in main()
const unsigned int SIZE = 10;


even though the compiler can count how many items are in the array, the program becomes fragile if this size is not a const variable and used throughout the code:

double v[SIZE] = {10.0, 9.3, 4.4, 9.1, 3.4, 11.0,43.0, 0.0, 1.0, 0.0};

Edit: Don't mix unsigned and signed types, because SIZE is unsigned we nee to changed the for loops as well:

15
16
17
for (unsigned int i=startIndex;i<SIZE;i++){
		if (v[i]<minValue) {minValue=v[i]; minIndex=i;}
	}


You should also pass this SIZE variable to all functions that need it.

Try to avoid using printf. std::cout is safer, although more verbose.

If you want to have a variable number of elements in a container use std::vector

Good Luck !!
Last edited on Jan 18, 2017 at 1:51am
Jan 18, 2017 at 1:56am
BTW, I know this isn't the best way to do it, but I tried to learn how to use the <vector> and it seemed really over-complicated, I couldn't learn how to create a simple list, like a did below, using <vector>. So if someone could shed a light on that to me, I'd be very grateful...


Actually vectors are quite simple, look at the example code here:
http://www.cplusplus.com/reference/vector/vector/vector/
Jan 18, 2017 at 3:57am
> no return statement in function returning non-void
which refers to the `indexOfMin()' function.
you need to return minIndex; (which should be an integer, by the way)


> I couldn't learn how to create a simple list, like a did below, using <vector>
with c++11 std::vector<double> v {10.0, 9.3, 4.4, 9.1, 3.4, 11.0,43.0, 0.0, 1.0, 0.0};
Topic archived. No new replies allowed.