完成后,我的程序在结束时冻结

时间:2016-04-28 20:41:37

标签: c++

我一直在为一个插入排序的东西上课,我正在玩这个东西。但是,当我运行我的程序时,它似乎在大多数情况下工作正常(返回0;在main中然后冻结)。我不知道为什么或如何冻结,这让我很困惑。

如果你不计算generateNums,那么从insertSort对象调用的唯一方法就是sort。

Main.cpp的:

#include "stdafx.h"
#include "insertionSort.h"
#include <iostream>


int main()
{
    int* gotNums;
    const int INT_LENGTH = 100;

    //Create new insertionSort object
    insertionSort insSort(INT_LENGTH); //Length: 100
    std::cout << "hey";

    return 0;
}

insertionSort.h:

#pragma once
class insertionSort
{
private:
    int* _nums; //Sorted and unsorted nums
    int _sortedBegins; //Point at which sorted items begin. This minus one is the first unsorted item
    int _length;
    void shiftUnsorted();
public:
    insertionSort(int length);
    int* getNums();
    void sortNums();
    void generateNums();
};

insertionSort.cpp

#include "stdafx.h"
#include "insertionSort.h"
#include <random>
#include <iostream>

//Constructor and destructors
insertionSort::insertionSort(int length)
{
    _nums = new int(length + 1); //+1 to accomodate empty space between sorted & unsorted.
    std::cout << "made arr ";
    _sortedBegins = length + 1;
    std::cout << "made sorted ";
    _length = length + 1;
    std::cout << "made len ";
    this->generateNums();
}


/* Custom functions */

//Populate array with new numbers
void insertionSort::generateNums() {
    for (int i = 0; i < _length - 1; i++) { //Don't fill the last array; it's an empty place for the sorted items.
        _nums[i] = rand() % 100 + 1; //generate number between 1 and 100
    }
    _nums[_length] = -1; //Create buffer
}

//The main part of it all - sort the array
void insertionSort::sortNums() {
    int currPos = _sortedBegins - 1; //Loop backwards through the unsorted items so that there is minimal shifting.
    int currInt;

    while (currPos > 0) {
        currInt = _nums[currPos];
        for (int i = _length; i > _sortedBegins; i++) {
            if (i < currInt) { //Time to shift the sorted to the left
                _nums[_sortedBegins - 1] = 0; //Shift buffer left
                for (int i2 = _sortedBegins + 1; i2 <= i; i2++) { //Shift main sorted content left
                    _nums[i2] = _nums[i2 + 1];
                }
                _nums[i] = currInt;
                break;
            }
        }
        currInt--;
    }
}

//Get nums from array
int* insertionSort::getNums() {
    return _nums;
}

//Shift unsorted items to the left to make way for new data in sorted. NOTE: does not assign new value to sortedBegins.
void insertionSort::shiftUnsorted() {
    for (int i = 0; i < _sortedBegins - 1; i++) {
        _nums[i] = _nums[i + 1];
    }
    _nums[_sortedBegins - 1] = 0;
    //And, it's hopefully shifted!
}

任何人都知道为什么这不能正常工作?

谢谢,

- 山姆

2 个答案:

答案 0 :(得分:4)

变化:

_nums[_length] = -1; //Create buffer

为:

_nums[_length - 1] = -1; //Create buffer

_nums的有效索引是0length-1。您的代码在数组外部写入,这会导致未定义的行为。

for中的sortNums循环看起来也不正确:

for (i = _length; i > _sortedBegins; i++) {

_length开始是没有意义的,这是在数组结束之后。并且添加甚至更远离数组。我还没有真正分析该循环中的逻辑,所以我不确定正确的代码是什么。但是你需要先确保你留在阵列内。

但由于您的程序目前不会调用sortNums,因此现在不会出现问题。

shiftUnsorted执行

_nums[_sortedBegins - 1] = 0;

如果_sortedBegins0,您将在数组外写字。你现在也不打电话给这个。当你添加对它的调用时,确保它在真的时候永远不会被调用,或者在函数中添加一个检查。

答案 1 :(得分:0)

这段代码对我来说很可疑:

_nums = new int(length + 1);

我希望方括号而不是圆括号。因为像这样,你只是创建一个指向 int的指针,该指针用值101初始化。但是你要创建一个101 int的数组,然后用值填充它。所以写下这个:

_nums = new int[length + 1];