So I'm getting a segmentation fault error in the beginning of the code. I've tried running some tests at the different points and the error seems to be when i allocate memory for the array. Ive just started learning about heap and stack memory so I'm not really sure if I'm doing something wrong there. Any help would be appreciated.
#include <iostream>
using namespace std;
//Function Prototypes
void sort(int A[], int n);
int findMin(int A[], int n, int j);
int swap(int& a, int& b);
double median(int A[], int n);
void output1(int median);
void output2(double median);
int main()
{
int size;
int array[size]; //Segmentaion fault here
int i = 0;
cout << "Enter the size of the list (< 1 to quit): ";
cin >> size;
while(size >= 1)
{
double element;
cout << "Enter element " << i+1 << ": ";
cin >> element;
array[i] = element;
i++;
while(i < size)
{
cout << "Enter element " << i+1 << ": ";
cin >> element;
array[i] = element;
i++;
}
sort(array, size);
median(array, size);
cout << "Enter the size of the list (< 1 to quit): ";
cin >> size;
}
delete [] array;
return 0;
}
void sort(int A[], int n)
{
int min;
for(int i = 0; i < n; i++)
{
min = findMin(A,n,i);
//min = findMinIndex(p, size, i);
//if(min )
swap(A[i],A[min]);
//swap(p[i],p[min]);
}
}
int findMin(int A[], int n, int j)
{
int minIndex = j;
for(int i = j+1; i < n; i++)
if(A[i]<A[minIndex])
minIndex = i;
return minIndex;
}
int swap(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void output1(int median)
{
cout << "The median is " << median << "." << endl;
}
void output2(double median)
{
cout << "The median is " << median << "." << endl;
}
double median(int A[], int n)
{
if(n % 2 == 0)
{
int div1 = n / 2;
int num1 = A[div1];
int num2 = A[div1 -1];
double median = (num1 + num2) / 2;
output2(median);
}
else
{
int div2 = n - 1;
int median = div2 / 2;
output1(median);
}
}
ANSWER. Signal 11, or officially know as "segmentation fault", means that the program accessed a memory location that was not assigned. That's usually a bug in the program.
A segmentation fault means your program tried to access something it was not supposed to. That's part of the problem. There are many reasons why a program can access things its not supposed to, but compiled code pretty much just says “#$#@^*&!” instead.
See if your compiler or library can be set to check bounds on [i] , at least in debug mode. Segmentation faults can be caused by buffer overruns that write garbage over perfectly good pointers. Doing those things will considerably reduce the likelihood of segmentation faults and other memory problems.
In practice, segfaults are almost always due to trying to read or write a non-existent array element, not properly defining a pointer before using it, or (in C programs) accidentally using a variable's value as an address (see the scanf example below).
Because you are not initialising size
, the value in that variable could literally be anything. If it happens to be excessively large, say 106,840,406, then you won't be able to get an int[]
of that size.
So basically, initialise your size
variable to something sensible.
Segmentation Fault 11 equals to say "Index out of range"...
Index
0, 1, 2, 3, 4 ,5
Value 5, 6 ,1 ,9 ,8 ,7
Array length is 6, but its last index is 5.. for example, if we control a for cycle with 6 then we got Segmentation Fault 11...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With