0

I've been having trouble figuring out just what is causing the overflow error within my C++ program using visual basic. Any help in a few pointers would be greatly appreciated.

#ifndef Specification_H
#define Specification_H
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <ctime>
#include <stdlib.h>
#include <time.h>
using namespace std;

class Specification {

private:

    int left;
    int right;
    long length = 1000;
    const long max_length = 300000;

public:
    Specification();
    void read();
    void sleep(clock_t);
    long partition(long,long);
    void bubbleSort();
    void quickSort(long,long);
    void insertionSort();
    bool stopped = true;

};
#endif

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include"Specification.h"

using namespace std; 
static long length = 1000; 
static const long max_length = 100000;
static int list[max_length];

Specification::Specification() {}

void Specification::read()
{
    ifstream fin("NumFile100K.txt", ios::binary); 
    for (long i = 0; i < length; i++)
    {
        fin.read((char*)&list[i], sizeof(int));
    }
    fin.close();
}

void Specification::bubbleSort()
{
    int temp; for (long i = 0; i < length; i++)
    {
        for (long j = 0; j < length - i - 1; j++)
        {
            if (list[j] > list[j + 1])
            {
                temp = list[j]; 
                list[j] = list[j + 1]; 
                list[j + 1] = temp;
            }
        }
    }
}

void Specification::insertionSort()
{
    int temp; for (long i = 1; i < length; i++)
    {
        temp = list[i]; 
        long j; 
        for (j = i - 1; j >= 0 && list[j] > temp; j--)
        {
            list[j + 1] = list[j];
        }
        list[j + 1] = temp;
    }
}

long Specification::partition(long left, long right)
{
    static int pivot_element = list[left]; 
    static int lb = left, ub = right;
    static int temp;
    while (left < right)
    {
        while (list[left] <= pivot_element)
            left++; 
        while (list[right] > pivot_element)
            right--; 
        if (left < right)
        {
            temp = list[left]; 
            list[left] = list[right]; 
            list[right] = temp;
        }
    }
    list[lb] = list[right]; 
    list[right] = pivot_element; 
    return right;
}

void Specification::quickSort(long left, long right)
{
    if (left < right)
    {
        long pivot = partition(left, right); 
        quickSort(left, pivot - 1); 
        quickSort(pivot + 1, right);
    }
}

void Specification::sleep(clock_t delayInMS) {
    clock_t targetTime = clock() + delayInMS;
    while (clock() < targetTime);
}

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include"Specification.h"
#include "StopWatch.h"

using namespace std;
static long length = 1000;
static const long max_length = 100000;
static int list[max_length];


int main() {

    Specification sort;
    StopWatch StopWatch;
    static double beginTime, endTime;

    for (length = 1000; length <= max_length;) {
        cout << "\nLength\t: " << length << '\n';
        sort.read();
        StopWatch.Start();
        beginTime = clock();
        sort.bubbleSort();
        StopWatch.Stop();
        endTime = clock();
        cout << "Bubble Sort\t: " << (endTime - beginTime) / CLK_TCK << " sec\n";
        sort.read();
        StopWatch.Start();
        beginTime = clock();
        sort.insertionSort();
        StopWatch.Stop();
        endTime = clock();
        cout << "Insertion Sort\t: " << (endTime - beginTime) / CLK_TCK << " sec\n";
        sort.read();
        StopWatch.Start();
        beginTime = clock();
        sort.quickSort(0, length - 1);
        StopWatch.Stop();
        endTime = clock();
        cout << "Quick Sort\t: " << (endTime - beginTime) / CLK_TCK << " sec\n";
        switch (length) {
        case 1000:
            length = 5000;
            break;
        case 5000:
            length = 10000;
            break;
        case 10000:
            length = 50000;
            break;
        case 50000:
            length = 100000;
            break;
        }
    }
    return 0;
}

The stack overflow error seems to occur within the "partition" method.

Yksisarvinen
  • 15,158
  • 2
  • 21
  • 46
jreuas
  • 1
  • 1
  • 2
    [How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) will be very helpful in this kind of situation. – Eljay Dec 12 '19 at 17:41
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Dec 12 '19 at 17:49
  • `while (list[left] <= pivot_element)` - initialize `list` with 100000 zeroes, its full size. Call `partition(0, 99999)` and watch the value of `left` reach the end of the array, and keep going, and going, and going, like the energizer bunny... – Sam Varshavchik Dec 12 '19 at 17:55
  • Using static variables within `partition` is wrong. They are only initialized the _first_ time the function is called. – 1201ProgramAlarm Dec 12 '19 at 18:37

0 Answers0