Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }
}
like image 728
PrivatePatron Avatar asked Jan 18 '17 21:01

PrivatePatron


People also ask

What does segmentation fault 11 mean in C?

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.

What is segmentation fault 11 on Mac?

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.

How do you fix a segmentation fault?

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.

What causes segmentation faults C?

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


2 Answers

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.

like image 127
Joe C Avatar answered Oct 15 '22 12:10

Joe C


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

like image 29
Mikeh Miiikeh Avatar answered Oct 15 '22 12:10

Mikeh Miiikeh