仅由非常大的随机输入引起的在排队列之前转储在pqueue堆中的分段故障核 你好,我是从斯坦福大学的CS106B编程抽象中得到的,它是C++中的编程程序,在PQueGub分配(优先级队列)(基于堆的实现)中,我有这种分割错误内核丢弃行为,当我从一个队列中把东西排入其中,我已经询问了大量的随机性质的条目,它所需的确切数量因运行而异,可以是10000到70000个条目之间的任何内容,如果我将排序输入(如lupe counter)排入队列,则不会出现问题,甚至可能是10000000个条目。我花了几个小时试图找到这个bug,但我找不到我的猜测是我必须在内存管理方面做一些错误,但是我使用的PQueue::doubleCapacity()方法是基于working vector类中的一个,我认为排队和退队方法都很好,因为PQUE对于仍然相当大的条目(少于10000条)仍然可以正常工作。这不是一个很容易用谷歌搜索的问题,所以代码如下。代码使用了CS106库中的一些分类,可以在中找到,但我希望它足够简单,因此您不需要编译它来告诉我我做错了什么
我正在使用g++开发Ubuntu 12.04,如果这很重要的话仅由非常大的随机输入引起的在排队列之前转储在pqueue堆中的分段故障核 你好,我是从斯坦福大学的CS106B编程抽象中得到的,它是C++中的编程程序,在PQueGub分配(优先级队列)(基于堆的实现)中,我有这种分割错误内核丢弃行为,当我从一个队列中把东西排入其中,我已经询问了大量的随机性质的条目,它所需的确切数量因运行而异,可以是10000到70000个条目之间的任何内容,如果我将排序输入(如lupe counter)排入队列,则不会出现问题,甚至可能是10000000个条目。我花了几个小时试图找到这个bug,但我找不到我的猜测是我必须在内存管理方面做一些错误,但是我使用的PQueue::doubleCapacity()方法是基于working vector类中的一个,我认为排队和退队方法都很好,因为PQUE对于仍然相当大的条目(少于10000条)仍然可以正常工作。这不是一个很容易用谷歌搜索的问题,所以代码如下。代码使用了CS106库中的一些分类,可以在中找到,但我希望它足够简单,因此您不需要编译它来告诉我我做错了什么,c++,segmentation-fault,priority-queue,C++,Segmentation Fault,Priority Queue,我正在使用g++开发Ubuntu 12.04,如果这很重要的话 /* * File: pqueue.h */ #ifndef _pqueue_h #define _pqueue_h #include "genlib.h" #include "vector.h" #include "disallowcopy.h" class PQueue { public: PQueue(); ~PQueue(); bool isEmpty(); int size()
/*
* File: pqueue.h
*/
#ifndef _pqueue_h
#define _pqueue_h
#include "genlib.h"
#include "vector.h"
#include "disallowcopy.h"
class PQueue
{
public:
PQueue();
~PQueue();
bool isEmpty();
int size();
void enqueue(int newElem);
int dequeueMax();
/*
* needed for assigment puprposses there are three more PQueue implementation
* so it is needed to compare memory speed trade of.
*/
int bytesUsed();
string implementationName();
void printDebuggingInfo();
bool printDebugToFile(string fileName);
private:
template <typename Type>
void PrintArry(Type arr[], int size);
DISALLOW_COPYING(PQueue)
static const int START_SIZE = 2;
int* entries;
int _capacity_, _size_;
void doubleCapacity();
void halfCapacity();
void swap(int& one, int &two);
};
#endif
// ---------------------------------------------------------------------------------------------
/*
* File: pqheap.cpp
* -- ----------------
*/
//#include "pqueue.h" // commented out so i can compile this whole file at once
#include "genlib.h"
#include <iostream>
#include <sstream>
#include <fstream>
PQueue::PQueue()
{
entries = new int[START_SIZE];
_capacity_ = START_SIZE;
_size_ = 0;
/*
* i am not using first cell so i want to know what should be in it this is because of
* the fact that in the way in which i have implemented heap based PQueue the acuall values
* are stored form number 1 and not 0; so child of value x can be reach by multiplying
* the index of it by two adding 1 for the second child;
*/
entries[_size_] = -888;
}
PQueue::~PQueue()
{
if (entries != NULL) delete[] entries;
}
bool PQueue::isEmpty()
{
return (_size_ == 0);
}
int PQueue::size()
{
return _size_;
}
/*
* the heap enqueuing works by adding new value to the end of the array and bubrling it up to the
* wright position by compare with its parent and swap if necesery.
*/
void PQueue::enqueue(int newValue)
{
int curValPos = ++_size_;
if(_size_ == _capacity_)
doubleCapacity();
entries[curValPos] = newValue;
// bubbling value up to its proper position;
while(curValPos > 1)
{
int parentPos = curValPos/2;
if(newValue < entries[parentPos])
break;
else
{
swap(entries[curValPos], entries[parentPos]);
curValPos = parentPos;
}
}
}
/*
* dequeuing is done by taking the highest value ( value from index 1 ) and then to repare the
* queue last value is copied to the first position and then buubled down till it reaches
* its proper position, it is done by comparison with children of the current position and
* swaping while nesessery.
*/
int PQueue::dequeueMax()
{
if (isEmpty())
Error("Tried to dequeue max from an empty pqueue!");
if(_capacity_ > (_size_*2)+10)
halfCapacity();
int curPos = 1;
int maxValue = entries[curPos];
//cout << maxValue << "|" ;
entries[curPos] = entries[_size_];
_size_ -= 1;
int biggerChild = 0;
while(biggerChild < _size_)
{
biggerChild = curPos*2;
if(entries[biggerChild] < entries[biggerChild+1])
biggerChild += 1; // the second child is bigger than the first
// if the bigger child is smaller than newVal or if the current
// position does not have children
if(entries[curPos] >= entries[biggerChild] || biggerChild > _size_)
break;
else
{
swap(entries[curPos], entries[biggerChild]);
curPos = biggerChild;
}
}
return maxValue;
}
int PQueue::bytesUsed()
{
cout << endl << "______________________ " << endl;
cout << "SIZE OF THIS = " << sizeof(*this) << endl;
cout << "SIZE OF ENTRIES = " << sizeof(*entries) << endl;
cout << "______________________ " << endl;
return sizeof(*this) + sizeof(int)*_size_;
}
string PQueue::implementationName()
{
return "( heap )";
}
void PQueue::printDebuggingInfo()
{
cout << "------------------ START DEBUG INFO ------------------" << endl;
cout << "Pqueue contains " << _size_ << " entries" << endl;
for (int i = 1; i <= _size_; i++)
cout << entries[i] << " ";
cout << endl << endl;
/*
* the print out is helpful only for the top few nodes and children.
*/
int numInCurRow = 1 , numInPrewRow = 1;
for (int i = 1; i <= _size_; i++)
{
cout << entries[i] << "|";
numInCurRow--;
if(numInCurRow == 0)
{
cout << endl;
numInPrewRow *= 2;
numInCurRow = numInPrewRow;
}
}
cout << endl;
cout << "------------------ END DEBUG INFO ------------------" << endl;
}
bool PQueue::printDebugToFile(string fileName)
{
ofstream outFile;
string str = fileName;
outFile.open(str.c_str());
if(outFile.fail())
{
cout << "WriteVectorToFile could not open file " + fileName << endl;
return false;
}
outFile << "------------------ START DEBUG INFO ------------------" << endl;
outFile << "Pqueue contains " << _size_ << " entries" << endl;
for (int i = 1; i <= _size_; i++)
outFile << entries[i] << " ";
outFile << endl << endl;
int numInCurRow = 1 , numInPrewRow = 1;
for (int i = 1; i <= _size_; i++)
{
outFile << entries[i] << "|";
numInCurRow--;
if(numInCurRow == 0)
{
outFile << endl;
numInPrewRow *= 2;
numInCurRow = numInPrewRow;
}
}
outFile << endl;
outFile << "------------------ END DEBUG INFO ------------------" << endl;
outFile.close();
return true;
}
void PQueue::doubleCapacity()
{
_capacity_ *= 2;
int* biggerArry = new int[_capacity_];
cout << "resizing capacity from " << _capacity_/2 << " new capacity = " << _capacity_ << endl;
for(int i = 0; i <= _size_; i++)
biggerArry[i] = entries[i];
delete[] entries;
entries = biggerArry;
}
/*
*
*/
void PQueue::halfCapacity()
{
_capacity_ /= 2;
int* halfArry = new int[_capacity_];
cout << endl <<" downsizing capacity from " << _capacity_*2 << " new capacity = " << _capacity_ << endl;
for(int i = 0; i < _capacity_; i++)
halfArry[i] = entries[i];
delete[] entries;
entries = halfArry;
}
void PQueue::swap(int &one, int &two)
{
int tmp = one;
one = two;
two = tmp;
}
//---------------------------------------------------------------------------------------------
/*
* main.cpp the driver
*/
/* File: main.cpp
* --------------
* Simple main module for PQueue assignment.
*/
//#include "pqheap.cpp" // commented out so i can compile this whole file at once
#include <iostream>
#include "genlib.h"
#include "simpio.h"
#include "random.h"
#include <ctime>
#include <fstream>
#include "vector.h"
using namespace std;
/*
* auxiliary functions
*/
string iToS(int x);
int sToI(string str);
bool ReadVectorFromFile(Vector<int> &v, string fileName);
template <typename Type>
bool WriteVectorToFile(Vector<Type> v, string fileName);
int main()
{
Randomize();
PQueue pq;
while(true)
{
cout << "how big queue do we want to work with ? "+pq.implementationName() << endl;
int y, x = GetInteger();
Vector <int> v;
/*
* "1000000.vec" file contains 1000000 unsorted integers with no repetitions,
* use of it produces the same segmentation fault core dumped it is cometed out for now
*/
//ReadVectorFromFile(v,"1000000.vec");
double start = double(clock())/1000000;
for(int i = 0; i < x; i++)
{
pq.enqueue(RandomInteger(0,i));
}
double stop = double(clock())/1000000;
cout << "time needed for enqueue of size "<< x << " = " << stop-start << endl;
//v.clear();
pq.printDebugToFile("debug."+ iToS(x)+ ".debug");
/*
* it seems that even dequeung a few values from the top produces the error
*/
cout << "how much to dequeue ?" << endl;
y = GetInteger();
start = double(clock())/1000000;
for(int i = 0; i < y; i++)
{
//pq.printDebuggingInfo();
v.add(pq.dequeueMax());
}
stop = double(clock())/1000000;
cout << "time needed for dequeue "+iToS(y)+" elements from PQ of size "<< x << " = " << stop-start << endl;
WriteVectorToFile(v, "QUEUE_" + iToS(x) + ".test");
}
return (0);
}
//---------------------------------------------------------------------------------
string iToS(int x)
{
ostringstream convert;
convert << x;
return convert.str();
}
int sToI(string str)
{
istringstream converter(str);
int n;
converter >> n;
return n;
}
bool ReadVectorFromFile(Vector<int> &v, string fileName)
{
ifstream inFile;
inFile.open(fileName.c_str());
if(inFile.fail())
{
cout << " ReadVectorFromFile could not read from the file " + fileName << endl;
return false;
}
string line;
while(true)
{
getline(inFile, line);
if(inFile.fail())
break;
v.add(sToI(line));
}
inFile.close();
return true;
}
template <typename Longboard>
bool WriteVectorToFile(Vector<Longboard> v, string fileName)
{
ofstream outFile;
string str = fileName;
outFile.open(str.c_str());
if(outFile.fail())
{
cout << "WriteVectorToFile could not open file " + fileName << endl;
return false;
}
for(int i = 0; i < v.size(); i++)
{
outFile << v[i];
outFile << endl;
}
outFile.close();
return true;
}
/*
*文件:pqueue.h
*/
#如果没有
#定义
#包括“genlib.h”
#包括“vector.h”
#包括“disallowcopy.h”
类PQUE
{
公众:
PQueue();
~PQueue();
bool是空的();
int size();
无效排队(int-newElem);
int dequeueMax();
/*
*对于assignment purposes,还需要三个PQUE实现
*因此,需要比较交易的内存速度。
*/
int字节使用();
字符串实现名称();
void printDebuggingInfo();
bool printDebugToFile(字符串文件名);
私人:
模板
void PrintArry(类型arr[],整数大小);
不允许复制(PQueue)
静态常量int START_Usize=2;
int*条目;
整数(容量),整数(大小);;
无效容量();
空隙半承载力();
作废互换(int&one、int&2);
};
#恩迪夫
// ---------------------------------------------------------------------------------------------
/*
*文件:pqheap.cpp
* -- ----------------
*/
//#包括“pqueue.h”//注释掉,这样我可以一次编译整个文件
#包括“genlib.h”
#包括
#包括
#包括
PQueue::PQueue()
{
条目=新整数[起始大小];
_容量=开始大小;
_尺寸=0;
/*
*我没有使用第一个电池,所以我想知道它应该是什么,这是因为
*事实上,在我实现基于堆的PQUE的方式中,acuall值
*是以数字1而不是0的形式存储的;因此可以通过相乘来访问值x的子级
*第二个孩子的指数为2加1;
*/
条目[\u大小]=-888;
}
PQUE::~PQUE()
{
如果(entries!=NULL)删除[]个条目;
}
boolpqueue::isEmpty()
{
返回值(_size_==0);
}
int PQueue::size()
{
返回大小;
}
/*
*堆排队的工作方式是向数组末尾添加新值,然后将其bubring到
*通过与其父级进行比较,并在必要时交换位置。
*/
void PQueue::排队(int newValue)
{
int curValPos=++\u size;
如果(\u大小\u==\u容量)
双容量();
条目[curValPos]=新值;
//起泡值达到其适当位置;
而(曲线坡度>1)
{
int parentPos=curValPos/2;
if(newValue cout乍一看,您使用的TL;DR!请缩小范围!我建议您使用调试器找到崩溃的位置,然后编辑您的问题,使其仅包含相关代码。如果问题看起来是随机的,那么您可能在调用未定义的行为,可能是写入超出分配内存的范围,或者使用指向的指针返回的函数的局部变量。谢谢Joachim在您的评论之后,我确实发现了错误,并且它确实是在访问未分配的内存,因为在退出队列后修复堆的循环中,由于中断条件下的错误逻辑,它试图将当前位置的值与远处的值进行比较ild节点远远超出了范围,应该被阻止,现在谢谢。@JoachimPileborg,请您将您的评论作为答案接受,以便将此问题标记为“已回答”