6

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);
    }
}
max66
  • 65,235
  • 10
  • 71
  • 111
PrivatePatron
  • 81
  • 1
  • 1
  • 5
  • 3
    so, using a debugger, where *exactly* does the segfault happen? – Marcus Müller Jan 18 '17 at 21:33
  • 2
    The size of a non-dynamic array must have a constant size known at compile-time. Something like `int size; int array[size];` is illegal C++. Plus you get the segfault because `size` is not initialized. – DeiDei Jan 18 '17 at 21:36
  • Note that even with a compiler that allows this (such as GCC or Clang), you're using a non-standard extension; while variable-length arrays are a (currently optional, I believe) part of the C standard, they aren't in the C++ standard at all. – Justin Time - Reinstate Monica Jan 19 '17 at 19:45
  • [this](https://stackoverflow.com/a/77336/2794395) may help you on findout who fires the exception. – fiorentinoing May 16 '18 at 14:11

3 Answers3

10

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.

Joe C
  • 15,324
  • 8
  • 38
  • 50
4

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...

2

An array in c++ has to be initialized at the with a fixed size. In your case, size is not initialized to any fixed integer value, which is illegal in c++ and will cause the compiler to produce an error message.

If you try the following line just before you initialize the array of size size, you can tell what the size originally is:

    cout << size << endl;

I compiled your code with this line and got this int size before the compiler failed:

1995231824 (This differs for every compiler and computer, but every number will be as big and useless as this one)

Trying to have such a big array will naturally lead to a segmentation fault. That's why you would have to initialize the variable size to a fixed number. This will eliminate the segmentation fault.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31