2012-12-11 7 views
0

안녕 얘들 아 나는 일종의 일하고있어 거품 정렬, 병합 정렬 및 셸 정렬을 구현하려고합니다. 나는 오래된 기술을 사용하지만 너희들이 내가 왜 계속해서 다음과 같은 오류가 나오는지 알려줄 수 있는지 궁금해하고있다.FirstChance 예외 StackOverFlow 병합 정렬 셸 버블 정렬 정렬

sortApplication2.exe에서 0x01135EF7의 첫 번째 예외 : 0xC00000FD : 스택 오버플로 (매개 변수 : 0x00000000, 0x00542000) . sortApplication2.exe의 0x01135EF7에서 처리되지 않은 예외가 발생했습니다. 0xC00000FD : 스택 오버플로 (매개 변수 : 0x00000000, 0x00542000).

Visual Studio 2012에서 어떤 부분이 재생되면 사용하고 있습니다. 내 코드는 세 가지 다른 파일에 있으므로 각 파일을 별도로 게시 할 것입니다.

내 헤더 파일 :

#pragma once 
class sort 
{ 
public: 
    sort(); 
    void random1(int array[]); 
    void random2(int array[]); 
    void random3(int array[]); 
    void bubbleSort(int array[], int length); 
    /*void merge(int *input, int p, int r);           
    void merge_sort(int *input, int p, int r);*/ 
    void shellSort(int array[], int length); 
}; 

내 클래스 구현 파일 :

#include "sort.h" 
#include <time.h> 
#include <iostream> 

using namespace std; 

sort::sort() 
{} 

void sort::random1(int array[]) 
{ 
     // Seed the random-number generator with current time so that 
     // the numbers will be different every time the program runs. 
    for(int i = 0; i < 25; i++) 
    { 

     srand ((unsigned) time(NULL)); 
     int n = rand();  //generates a random number 
     array[i] = n;  //places it into the array 
    } 

} 
void sort::random2(int array[]) 
{ 
    // Seed the random-number generator with current time so that 
    // the numbers will be different every time the program runs. 
    for(int i = 0; i < 10000; i++) 
    { 
     srand ((unsigned) time(NULL)); 
     int n = rand();  //generates a random number 
     array[i] = n;  //places it into the array 
    } 

} 
void sort::random3(int array[]) 
{ 
    // Seed the random-number generator with current time so that 
    // the numbers will be different every time the program runs. 
    for(int i = 0; i < 100000; i++) 
    { 
     srand ((unsigned) time(NULL)); 
     int n = rand();  //generates a random number 
     array[i] = n;  //places it into the array 
    } 
} 
void sort::bubbleSort(int array[], int length) 
{ 
    //Bubble sort function 

    int i,j; 

    for(i = 0; i < 10; i++) 
    { 
     for(j = 0; j < i; j++) 
     { 
      if(array[i] > array[j]) 
      { 
       int temp = array[i]; //swap 
       array[i] = array[j]; 
       array[j] = temp; 
      } 
     } 
    } 
} 

    /*void sort::merge(int* input, int p, int r) //the merge algorithm of the merge sort 
    { 
     int mid = (p + r)/2; 
     int i1 = 0; 
     int i2 = p; 
     int i3 = mid + 1; 

    // Temp array 
    int x = r -p + 1; 
    int *temp; 
    temp = new int [x]; 

    // Merge in sorted form the 2 arrays 
    while (i2 <= mid && i3 <= r) 
     if (input[i2] < input[i3]) 
      temp[i1++] = input[i2++]; 
     else 
      temp[i1++] = input[i3++]; 
     // Merge the remaining elements in left array 
    while (i2 <= mid) 
     temp[i1++] = input[i2++]; 

    // Merge the remaining elements in right array 
    while (i3 <= r) 
     temp[i1++] = input[i3++]; 

    // Move from temp array to master array 
    for (int i = p; i <= r; i++) 
     input[i] = temp[i-p]; 
} 
void sort::merge_sort(int *input, int p, int r) //the merge sort algorithm 
{ 
    if (p < r) //When p and r are equal the recursion stops and the arrays are then  passed to the merge function. 
    { 
     int mid = (p + r)/2; 
     merge_sort(input, p, mid); //recursively calling the sort function in order to  break the arrays down as far as possible 
     merge_sort(input, mid + 1, r);//recursively calling the sort function in order to  break the arrays down as far as possible 
     merge(input, p, r); //merge function realigns the smaller arrays into bigger arrays  until they are all one array again 
    } 
}*/ 


void sort::shellSort(int array[], int length) //Shell sort algorithm 
{ 
    int gap, i, j, temp; 
    for(gap = length/2; gap > 0; gap /= 2) //gap is the number of variables to skip when doing the comparisons 
    { 
     for(i = gap; i < length; i++) //This for loop sets the variable to use as the gap for the comparisons 
     { 
      for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j -= gap) 
      { 
       temp = array[j]; //the array variables are swapped 
       array[j] = array[j + gap]; 
       array[j + gap] = temp; 
      } 
     } 
    } 
} 

그리고 내 드라이버 파일 :

#include "sort.h" 
#include <iostream> 

using namespace std; 

int main() 
{ 
    int bubbleArray1[25]; //these are the arrays to be sorted. three for each sort.  each has a length of 25, 10000, or 100000. 
    int bubbleArray2[10000]; 
    int bubbleArray3[100000]; 
    int mergeArray1[25]; 
    int mergeArray2[10000]; 
    int mergeArray3[100000]; 
    int shellArray1[25]; 
    int shellArray2[10000]; 
    int shellArray3[100000]; 
    sort Sorts; 

    Sorts.random1(bubbleArray1); 
    Sorts.random1(mergeArray1); 
    Sorts.random1(shellArray1); 
    Sorts.random2(bubbleArray2); 
    Sorts.random2(mergeArray2); 
    Sorts.random2(shellArray2); 
    Sorts.random3(bubbleArray3); 
    Sorts.random3(mergeArray3); 
    Sorts.random3(shellArray3); 

    cout << "BubbleSort1 is now being sorted.\n"; 
    Sorts.bubbleSort(bubbleArray1, 25); 
    cout << "BubbleSort2 is now being sorted.\n"; 
    Sorts.bubbleSort(bubbleArray2, 10000); 
    cout << "BubbleSort3 is now being sorted.\n"; 
    Sorts.bubbleSort(bubbleArray3, 100000); 
    cout << "End bubble sorts.\n"; 

    /*cout << "MergeSort1 is now being sorted.\n"; 
    Sorts.merge_sort(mergeArray1, 0, 25); 
    cout << "MergeSort2 is now being sorted.\n"; 
    Sorts.merge_sort(mergeArray2, 0, 10000); 
    cout << "MergeSort3 is now being sorted.\n"; 
    Sorts.merge_sort(mergeArray3, 0, 100000); 
    cout << "End merge sorts.\n";*/ 

    cout << "ShellSort1 is now being sorted.\n"; 
    Sorts.shellSort(shellArray1, 25); 
    cout << "ShellSort1 is now being sorted.\n"; 
    Sorts.shellSort(shellArray2, 10000); 
    cout << "ShellSort1 is now being sorted.\n"; 
    Sorts.shellSort(shellArray3, 100000); 
    cout << "End shell sorts.\n"; 

    cout << "Array\tElements\n"; 
    cout << "BubbleSort1\t"; 
    for(int i = 0; i < 25; i++) 
    { 
     cout << bubbleArray1[i] << " "; 
    } 
    cout << "\nMergeArray1\t"; 
    for(int i = 0; i < 25; i++) 
    { 
     cout << mergeArray1[i] << " "; 
    } 
    cout << "\nShellArray1\t"; 
    for(int i = 0; i < 25; i++) 
    { 
     cout << shellArray1[i] << " "; 
    } 


    return 0; 
} 

나는 코드를 많이 알고. 그리고 코드를 더 잘 만들 수있는 많은 방법이있을 것입니다. 난 내 컴파일러를 사용하여 찾을 수 없기 때문에 위의 오류의 원인을 알고 싶습니다.

+2

코드를 * 가장 짧은 * 샘플로 좁히려 고 시도해보십시오. –

+0

스택 트레이스가 도움이 될 것입니다 ... –

답변

0

스택에 너무 많은 메모리를 할당하고 있습니다. '자동'스토리지 클래스가있는 변수가 스택에 저장됩니다. 대신 힙을 할당하십시오.

그래서, 대신 :

int shellArray3[100000]; 

해야 할 것 :

int* shellArray3 = new int[100000]; 

또는 더 나은 아직, 표준 : : 벡터를 사용합니다.

힙 메모리를 사용하지 않으려는 경우 이와 같이 정적 저장소 클래스를 사용할 수도 있습니다. 이렇게하려면 그 : 오히려 스택에 각 기능 항목에 대한 사본을 할당하는 것보다 전체 프로그램에 대한 변수의 인스턴스를 할당합니다

static int shellArray3[100000]; 

.

+0

[array]를 삭제하는 것을 잊지 마십시오! –