gpt4 book ai didi

c++ - 我的程序在结束后卡住

转载 作者:行者123 更新时间:2023-11-30 01:43:59 24 4
gpt4 key购买 nike

我一直在为我正在玩的插入排序类编写一个类。但是,当我运行我的程序时,它似乎在大多数情况下都运行良好(返回 0;在 main 中然后卡住)。我不知道它为什么或如何卡住,这让我很困惑。

如果不计算 generateNums,从 insertionSort 对象调用的唯一方法是排序。

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;
}

插入排序.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();
};

插入排序.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!
}

有人知道为什么这不能正常工作吗?

谢谢,

-山姆

最佳答案

改变:

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

到:

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

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

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

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

_length 开始是没有意义的,它在数组末尾之后。并且向其添加 甚至超出数组范围。我还没有真正分析过那个循环中的逻辑,所以我不确定正确的代码是什么。但您首先需要确保自己留在阵列内。

但由于您的程序当前未调用 sortNums,因此现在不会造成问题。

shiftUnsorted 执行时

_nums[_sortedBegins - 1] = 0;

如果 _sortedBegins0,您将在数组外写入。你目前也不这么调用它。当您向它添加调用时,请确保它永远不会被调用,或者在函数中添加对此的检查。

关于c++ - 我的程序在结束后卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36925081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com