hello,
I have a Quicksort c++ program and I have tried to run it and it gives me an error calling it "segmentation fault" , i know there is somthing wrong in my code but can't figure it out. anyways any replies for a solution will be appreciated. thank you

#include <iostream>
#include<stdlib.h>
using namespace std ;
class SortList
{
private :
int *list;
int size;
public :
SortList(int size)
{
list=new int[size];
this->size=size;
}
void Swap(int i,int j)
{
int tmp;
tmp=list[i];
list[i]=list[j];
list[j]=tmp;
}
void SortList::QuickSort(int,int);
void StoreNum(int);
void Display();
};
void SortList::StoreNum(int x)
{
static int i=0;
*(list+i)=x;
i++;
}
void SortList::Display()
{
int i=0;
for(;i<size;i++)
cout<<*(list+i)<<" ";
}
void SortList::QuickSort(int X,int I)
{
int L,R,V;
if (I>X)
{
V=list[I];
L=X-1;
R=I;
for(;;)
{
while(list[++L]>V);
while(list[--R]<V);
if (L==R) break;
Swap(L,R);
}
Swap(L,I);
QuickSort(X,L-1);
QuickSort(L+1,I);
}
}

void main()
{
int i,x,n;
SortList *S;
system("cls");
cout<<endl<<"\t"<<"Program for Quick Sort"<<endl;
cout<<endl<<"How many numbers to be sorted : ";
cin>>n;
S=new SortList(n);
for(i=0;i<n;i++)
{
cout<<endl<<"Enter number : ";
cin>>x;
S->StoreNum(x);
}
cout<<endl<<"Numbers entered are "<<endl;
S->Display();
S->QuickSort(1,n-1);
cout<<endl<<"Numbers After QuickSort are "<<endl;
S->Display();
}

does the code compile with no errors? It does not with my compiler. You have to fix all the errors before attempting to run the program.

>> void main()

main never ever returns void. The correct way to dealre that function is like this -- any other way is wrong.:

int main()
{
    // your code here


    return 0;
}

i is not defined in quicksort. I think that the problem is in quicksort func.

does the code compile with no errors? It does not with my compiler. You have to fix all the errors before attempting to run the program.

>> void main()

main never ever returns void. The correct way to dealre that function is like this -- any other way is wrong.:

int main()
{
    // your code here
 
 
    return 0;
}

Yeah i use visual express edition and it didnt give me any errors while compiling, just when i enter the array of numbers, it says segmentation fault.
P.S
What is the library process.h, its not used on a cygwin system is it? if i want to what to i replace it with. because its concerning my program.

*process.h - definition and declarations for process control functions
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* This file defines the modeflag values for spawnxx calls.
* Also contains the function argument declarations for all
* process control related routines.
*
* [Public]
*
****/

For linux you will use whatever header file declares the spawn() family of functions.

Member Avatar for iamthwee

When I use new I always have to few up the memory using delete somewhere after.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.