So i need help figurin out where my segmentation fault is comin from. Im not even close to finishing this program but i was just testing my progress so far to make sure that it was taking values correctly, but sometimes the program doesnt stop when its supposed to and when it does i get a segmentation fault at the end, even if it works the way i want it to.

heres the code:

using namespace std;

class Heapsort
     int mylist[];
     int mysize;
     void get_size();
     bool empty();
     void sort();
     void fill_array();
     void print(int);

void Heapsort::get_size()
   cout <<endl<<"Please enter the size of the array you want to sort: ";
   cin >> mysize;
   int mylist[mysize];

void Heapsort::fill_array()
    int i;
    int num;

    for (i=0; i < mysize; i++){
       cout << "Enter a number 1-100: ";
       cin >> num;
       if ((num < 1)||(num > 100)){
           cout << "\n\nEnter a correct number please.\n\n";
           i = i - 1;
       } else {
           mylist[i] = num;

int main()
  Heapsort heap;


  cout << endl << endl;


  cout << endl << endl;
  return (0);

Edited by bigmaq: messed up my tags

8 Years
Discussion Span
Last Post by bigmaq

I'm not sure if this is related to your problem, but I'm pretty sure you aren't allowed to just declare arrays of a given size on demand:

cout <<endl<<"Please enter the size of the array you want to sort: ";   
cin >> mysize;   
//no bueno
int mylist[mysize];

I think you have to either be up front and declare an array of an arbitrary size, or create a dynamic array of whatever size you want.. but trying to declare an array of variable size is not allowed.

Try this instead:

cout <<endl<<"Please enter the size of the array you want to sort: ";   
cin >> mysize;   
//simple dynamic array 
int *mylist = new int[mysize];

Edited by Clinton Portis: your mom.


alright thanks, i completely forgot about dynamic arrays lol. ill try that and see if it fixes the seg. fault.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.