[英]My code runs fine when I debug it, but crashes as soon as I run it
I have been asked to built a K sorted array by my professor and then sort it using minheap. 我被要求由我的教授构建一个K排序数组,然后使用minheap对其进行排序。 A k sorted array is basically an array where each element is at most K positions away from the location it would be if the array was fully sorted in ascending order.
k排序的数组基本上是一个数组,其中每个元素最多K个位置,远离数组按升序完全排序的位置。 For instance, if K=3, the element at position i=8 can be at locations 5, 6, 7, 8, 9, 10, or 11 in the fully sorted arrayI'm stuck in the first part of the problem.
例如,如果K = 3,则位置i = 8处的元素可以位于完全排序的阵列中的位置5,6,7,8,9,10或11处于问题的第一部分中。 Requirements:
要求:
Allow the user to enter a number of (N) elements and the number K. 允许用户输入多个(N)元素和数字K.
Produce a K-sorted array based on the user input (more than one K-sorted array can be produced; randomly pick one). 根据用户输入生成K排序数组(可以生成多个K排序数组;随机选择一个)。 Display it.
显示它。
After rigorous thinking I built the below code for K Sorting: 经过严格的思考后,我为K排序构建了以下代码:
void kSortedArray:: displayKSorted()
{
cout<<"Please enter the no. of elements you want to KSort: ";
cin>>N;
cout<<"Please enter the value for K: ";
cin>>K;
int i=0;
while (i<N)
{
cout<<"Enter element "<<i+1<<" : ";
cin>>arr[i++];
}
insertion_sort(arr,N);
srand(time(NULL));
int output=0;
i=0;
int maxRand=0;
int minRand=0;
int arr2[N];
int a=0;
int found=0;
int found1=0;
int range=0;
int count1=0;
vector<int>:: iterator it1;
vector<int>:: iterator it2;
vector<int> arr3;
while(kSortedIndex.size()<N)
{
minRand=range-K;
maxRand=range+K;
output = minRand + (rand() % (maxRand - minRand + 1));
if (output<0)
{
output=N+output;
}
if(output>N-1)
{
output=output-N;
}
//arr2[i]=output;
for(it1=kSortedIndex.begin();it1!=kSortedIndex.end();it1++)
{
if(*it1==output)
{
found=1;
for(it2=arr3.begin();it2!=arr3.end();it2++)
{
if(*it2==output)
{
found1=1;
break;
}
}
if(found1==0)
{
arr3.push_back(output);
count1++;
}
break;
}
}
if(found==0)
{
kSortedIndex.push_back(output);
count1=0;
arr3.clear();
range++;
}
found=0;
found1=0;
if (count1>((2*K)+1))
{
range--;
count1=0;
}
if(range>(N-1))
range--;
}
vector<int>::iterator it;
int j=0;
// i=0;
for(it=kSortedIndex.begin();it!=kSortedIndex.end();it++)
{
arr2[*it]=arr[j];
j++;
//cout<<arr2[j++];
}
for(j=0;j<N;j++)
{
cout<<arr2[j];
}
}
Below is the header file: 以下是头文件:
class kSortedArray
{
public:
kSortedArray();
void displayKSorted();
protected:
private:
int N=0;
int K=0;
int* arr=new int [N];
createKSorted();
void insertion_sort(int arr[],int length);
vector <int> kSortedIndex;
};
Could someone please help me to figure out why my code crashes when I run it and runs fine when I debug it. 有人可以帮我弄清楚为什么我的代码在运行时崩溃并在我调试时运行正常。 Is it a memory leak issue?
这是内存泄漏问题吗? I even tried deleting arr, arr2 and clearing arr3 but that isn't working (code still cashes).
我甚至尝试删除arr,arr2和清除arr3,但这不起作用(代码仍然是兑现)。 Your help much appreciated.
非常感谢您的帮助。
The declaration int* arr=new int [N];
声明
int* arr=new int [N];
in the header is wrong. 在标题中是错误的。 You cannot allocate memory for the array here because you do not yet know the value for
N
. 您无法在此为阵列分配内存,因为您还不知道
N
的值。 That value will be known only after cin>>N;
只有在
cin>>N;
之后才知道这个值cin>>N;
in the kSortedArray:: displayKSorted
function. 在
kSortedArray:: displayKSorted
函数中。 Therefore you need to do allocate memory there: 因此你需要在那里分配内存:
void kSortedArray:: displayKSorted()
{
cout<<"Please enter the no. of elements you want to KSort: ";
cin>>N;
arr = new int [N]; // <<< add this line
cout<<"Please enter the value for K: ";
cin>>K;
int i=0;
...
Header: 标题:
...
private:
int N=0;
int K=0;
int* arr; // =new int [N]; <<< remove the allocation
createKSorted();
void insertion_sort(int arr[],int length);
...
Disclaimer: This is non tested non error checking code, and there may be more problems. 免责声明:这是未经测试的非错误检查代码,可能存在更多问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.