我在为学校作业编写的C ++程序中遇到了一个非常奇怪的错误(最后粘贴的代码),我无法弄清楚它为什么会这样做。特别是,有时给出随机错误的输出,有时给出正确的输出,每次都在相同的输入上运行。如果有人可能对原因有所了解,我会非常感激:
我做了一个C ++程序,它有一个简单的MaxHeap数据结构的实现,它支持使用HeapInsert构建堆,从空堆开始逐个地将元素插入堆中,或者从一个数组开始元素并在元素的前半部分使用bubbledown将其转换为堆 - 程序采用一个命令行参数,HeapInsert将使用构建堆的第一种方法,或者BubbleDown将使用第二种方法。
该程序从cin获取用户输入:首先是为了使堆出来而给出的元素数,然后是要放入堆中的元素。完成后,它会输出在冒泡/冒泡中执行的交换次数,然后输出堆的元素,以便它们位于存储堆的数组中。
我们已经获得了一个示例输入(100个随机数)和一个我的代码应该生成的示例输出,以便了解我们的实现是否正确。我在命令行上执行以下操作:
g++ HeapTest.cpp
./a.out BubbleDown < 100.txt > out
diff out s100b.txt
100.txt是样本输入,s100b.txt是正确的样本输出。
执行行
./a.out BubbleDown < 100.txt > out
diff out s100b.txt
反复来说,我的结果不一致。似乎有一半的时间我得到了我的输出完全匹配样本文件,但有一半时间它没有,特别是当我查看我的输出文件时,它看起来像一个随机的大数字已被插入我的堆中没有原因,使我的输出错误。
对我来说,完全没有意义的结果是在使用完全相同的输入重复运行代码时会不一致。这只发生在我在命令行上使用“BubbleDown”选项时。以下是我的代码:
#include <cstdlib>
#include <stdint.h>
#include <iostream>
#include <string>
#include <cstring>
#include <cassert>
#include <cmath>
using namespace std;
struct MaxHeap { //MaxHeap data structure
int n; //size of the heap
int numex; //number of exchanges in building the heap
int* A; //Array storing the actual heap
MaxHeap(int a){ //First Constructor: initializes an empty heap of size 0 in an array of size a
n=0; //initialize size to 0
numex=0;//initialize numex to 0
A = new int[a]; //allocate space for array of size A on heap
}
MaxHeap(int * data, int a){ //Second Constructor: consumes array of a elements and creates a heap
//out of thoses elements using bubbledown
n = a;
A = data;
numex = 0;
for(int k = (int)(floor((n-1)/2)); k > -1 ; k-=1){
bubbledown(k);
}
}
~MaxHeap(){} //necessary since MaxHeaps made with first constructor are non-contiguous
void bubbleup(int v){//bubble-up algorithm as described in class
int j;
while( (v != 0) && (A[(int)(floor((v-1)/2))] < A[v]) ){
numex +=1;
j = A[v];
A[v] = A[(int)(floor((v-1)/2))];
A[(int)(floor((v-1)/2))] = j;
v = (int)(floor((v-1)/2));
}
}
void bubbledown(int v){//bubbledown algorithm as described in calss
int j;
int k;
int L;
int temp;
while(true){
j = 2*v+1;
k = 2*v+2;
if((j <= n) && (A[j] > A[v])){L = j;}
else{L = v;}
if((k <= n) && (A[k] > A[L])){L = k;}
if(L == v){break;}
else{numex +=1; temp = A[v]; A[v] = A[L]; A[L] = temp; v=L;}
}
}
void HeapInsert(int i, int k){//heapinsert algorithm as described in class
n=k+1;
A[n-1] = i;
bubbleup(n-1);
}
};
void error(){
cerr << "Usage: " << endl;
exit(-1);
}
int main(int argc, char * argv[]){
int flag;
char hins[] = "HeapInsert";
char bdwn[] = "BubbleDown";
switch(argc){
case 2:
if(strcmp(argv[1], hins) == 0){flag=0; break;}
else if(strcmp(argv[1], bdwn) == 0){flag=1; break;}
else{error();}
default: error();
}
if(flag==0){//If HeapInsert option selected, the below creates a heap via HeapInsert
int nelem;
cin >> nelem; //read in number of elements that are going to be given
struct MaxHeap H = MaxHeap(nelem); //call first constructor
for(int k=0; k < nelem; k+=1){ //insert elements into the heap one by one as they are read in
int i;
cin >> i;
H.HeapInsert(i,k);
}
cout << H.numex << endl; //print number of exchanges
for(int k =0;k < nelem; k+=1){ //print elements of heap 1 by 1
cout << H.A[k] << endl;
}
}
else{ //if BubbleDown option chosen by user
int nelem;
cin >> nelem; //read in number of elements
int data[nelem]; //initialize array to store that number of elements
for(int k=0; k < nelem; k+=1){ //build array of elements in order given
int i;
cin >> i;
data[k] = i;
}
struct MaxHeap H = MaxHeap(data, nelem); //use second constructor to create a heap out of the array
cout << H.numex << endl; //print number of exchanges
for(int k =0;k < nelem; k+=1){ //print out elements 1 by 1
cout << H.A[k] << endl;
}
}
}
如果有人不知道我的代码如何在不依赖于任何随机性或内存分配(在给出BubbleDown选项时没有使用内存分配)的情况下产生不一致的结果,那么洞察将非常感激!< / p>
答案 0 :(得分:8)
我用调试符号编译你的程序......
gcc -g -O0 -o stuff stuff.cpp
并在Valgrind ...
中运行 echo '4 2 3 4 5 6' | valgrind ./stuff BubbleDown
以下是它的说法:
==28605== Conditional jump or move depends on uninitialised value(s)
==28605== at 0x401186: MaxHeap::bubbledown(int) (stuff.cpp:52)
==28605== by 0x400FCD: MaxHeap::MaxHeap(int*, int) (stuff.cpp:26)
==28605== by 0x400E08: main (stuff.cpp:125)
这似乎对应于此:
if((j <= n) && (A[j] > A[v])){L = j;}
问题似乎是你正在读取数组的末尾。如果是j == n
,那么它是过去一个元素的结尾。与k == n
相同。如果您将bubbledown
更改为此问题,则问题就会消失:
void bubbledown(int v){//bubbledown algorithm as described in calss
while(true){
const int j = 2*v+1;
const int k = 2*v+2;
int L;
// notice < instead of <=
if((j < n) && (A[j] > A[v])){
L = j;
}
else{
L = v;
}
// notice < instead of <=
if((k < n) && (A[k] > A[L])){
L = k;
}
if(L == v){
break;
}
else{
numex +=1;
const int temp = A[v];
A[v] = A[L];
A[L] = temp;
v = L;
}
}
}
注意:我使用了一些Linux命令来执行此操作(最重要的是Valgrind)。无论你使用什么编译器工具链/ IDE都应该有自己的调试器,可能会给你类似的输出。有一个Stack Overflow question about Valgrind substitutes in Windows。我建议找一个你喜欢的工具 - 它会使C ++调试更多更容易。