Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear all,

I'm stuck on code below and can't seem to figure out why it keeps crashing. It's supposed to be a simple quicksort algorithm in C but it's giving me a segmentation fault and crashing after I enter the input. I debugged it with Dev C++ and it shows that it's crashing in the recursive calls of quicksort. Can anyone point out where?

Also, for a program like this, can anyone point out a debugger or technique that will help me fix this myself? I have other such programs that I will need to test / practice and I don't want to keep having to post like this. Thanks all in advance.

C++
/* includes and macros */
// required for input / output with minGW GCC
#include <stdio.h>
#define printf __mingw_printf

// required for the random integer generator functions
#include <stdlib.h>
#include <time.h>


/* global declarations */
static unsigned short n;

/* function prototypes */
void swapSort(int *, int *);
void quickSortMid(int [], unsigned short, unsigned short);


int main()
{
    /* variable array declaration */
    printf("How many integers do you want sorted by Quick Sort? ");
    scanf("%hu", &n);
    int arrayInput[n];

    /* for rand() */
    srand(time(NULL));

    /* array input */
    printf("\n");
    for(register unsigned short i = 0; i <= n-1; i++)
        arrayInput[i] = rand();

    /* sort using the Quick Sort Medium algorithm */
    quickSortMid(arrayInput, 0, n-1);

    /* print the sorted array */
    printf("\n");
    for(register unsigned short i = 0; i <= n-1; i++)
        printf("[%d] ", arrayInput[i]);
    printf("\n");

    return 0;
}


/* function definitions */
void swapSort(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}


void quickSortMid(int intArray[], unsigned short low, unsigned short high)
{
    unsigned short e = high / 2, hole = e, ul = high;
    int temp = intArray[e];
    while(low < high)
    {
        if(hole >= e)
        {
            if(intArray[low] > temp)
            {
                swapSort(&intArray[low], &intArray[hole]);
                hole = low;
            }
            else low++;
        }
        else
        {
            if(intArray[high] < temp)
            {
                swapSort(&intArray[high], &intArray[hole]);
                hole = high;
            }
            else high--;
        }
    }
    intArray[hole] = temp;

    // recursive calls of Quick Sort to completely sort the array
    if(e >= 2 && (ul-e) >= 2)
    {
        quickSortMid(intArray, 0, e-1);
        quickSortMid(intArray, e+1, ul);
    }
}


What I have tried:

I have tried debugging with Dev C++ and have narrowed the problem to the recursive calls of the function but can't seem to figure out what's wrong with the logic.

Downloaded many different debuggers and read much on debugging but nothing that will help me learn properly how to debug my own programs. If this were a simple logic problem, then I could use printf to see where the sorting was wrong and why but the program is crashing.
Posted
Updated 19-Jul-16 6:32am

Quote:
Also, for a program like this, can anyone point out a debugger or technique that will help me fix this myself?

Any debugger will do.
You know what your program should do and how the variables should behave.

You need to execute your program line by line on debugger.
By inspecting the variables while the program runs, you will see if variable behave as expected or not.
where you find where the program stops to behave as expected, you are close to the bug.
When you have a complex expression that give wrong results, it is a good idea to add dummy lines that take parts of the expression just to expose the intermediate calc.

[Update]
when you start a program in debugger, you can run it like normal (like you do) or you can tell the debugger (in the menus) that you want to single step in the program.
I don't know the name in your debugger menu.
usually you have" names like step or trace
the debugger allow you to set breakpoints that will stop the program each time it reach that point.
 
Share this answer
 
v3
Comments
Aamir Yousafi 19-Jul-16 12:43pm    
What do you mean "line by line"? The debugger only points out where the segmentation fault happens. How do you execute the program line by line using the debugger?
Aamir Yousafi 19-Jul-16 13:05pm    
thanks
You can allocate the arrayinput this way, because n MUST be a constant to do it.

Make it dynamic like this code:
C++
int *arrayInput = new int[n];
//do your stuff

//cleanup
delete arrayInput;


Tip: dont mix ushort and int. It isnt faster, but leads to strange bugs.
 
Share this answer
 
Comments
Aamir Yousafi 19-Jul-16 13:06pm    
Thanks. I will incorporate that into my program(s).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900