简体   繁体   中英

Segmentation fault error 11 C++

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);
    }
}

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM