【发布时间】:2025-12-29 01:05:06
【问题描述】:
我有一个优先级队列数组,其中填充了“作业”(名称 + 优先级)。除了调整大小(如果它已满)之外,我已经能够让所有与队列相关的工作。这是我认为导致我无法弄清楚的分段错误的位。
编辑:
这里还有一些可以编译的代码,我保留了其余的函数,以防它们有任何帮助。现在初始容量设置为 5,当您尝试将作业添加到完整列表时,它将使阵列容量翻倍,并允许您在 SEG 之前添加更多作业。过错。
pq.h
#ifndef PQ_H
#define PQ_H
#include "interface.h"
#include <string>
using namespace std;
class Job {
public:
int getPriority();
string getTaskName();
void setPriority(int val);
void setTaskName(string tname);
Job();
private:
int priority;
string taskName;
};
class PriorityQueue {
public:
PriorityQueue();
~PriorityQueue();
int size();
bool isEmpty();
void clear();
void enqueue(string value, int priority);
string dequeue();
string peek();
int peekPriority();
PriorityQueue(const PriorityQueue & src);
PriorityQueue & operator=(const PriorityQueue & src);
private:
static const int INITIAL_CAPACITY = 5;
Job *array;
int count;
int capacity;
void expandCapacity() {
Job *oldArray = array;
capacity *= 2;
array = new Job[capacity];
for (int i = 0; i < count; i++) {
array[i] = oldArray[i];
}
delete[] oldArray;
}
};
#endif
pq.cpp
#include <iostream>
#include <cstring>
using namespace std;
//#include "job.h"
#include "pq.h"
Job::Job() // Constructor
{
priority= 0;
taskName = "There are no items in the list.";
}
int Job::getPriority(){ // returns the prority of the job
return priority;
}
string Job::getTaskName(){ // returns the name of the job
return taskName;
}
void Job::setPriority(int val){ // sets the priority of a newly created job
priority = val;
}
void Job::setTaskName(string tname){ // sets the name of a new job
taskName = tname;
}
PriorityQueue::PriorityQueue() // constructor
{
count = 0;
capacity = INITIAL_CAPACITY - 1;
array = new Job[INITIAL_CAPACITY];
}
PriorityQueue::~PriorityQueue() { // destructor
delete [] array;
}
int PriorityQueue::size() { // returns the number of jobs in the queue
return count;
}
bool PriorityQueue::isEmpty() { // returns true if queue is empty
if (count != 0){
return false;
}else{
return true;
}
}
void PriorityQueue::clear() { // clears queue of all jobs
count = 0;
// need to make it remove and delete the items
}
void PriorityQueue::enqueue(string value, int priority) {
// tests size to see if Queue is a max capacity
if(count == capacity){
expandCapacity();
cout << "\tList was full and has been expanded\n";
}
array[++count].setPriority(priority);
array[count].setTaskName(value);
// upheap operations
Job v = array[count];
int tempcount = count;
while (array[tempcount/2].getPriority() >= v.getPriority()){
array[tempcount] = array[tempcount/2];
tempcount = tempcount/2;
array[tempcount] = v;
}
}
string PriorityQueue::dequeue() {
// removes the job with the highest priority from the queue and returns the name
if(this->isEmpty()){ // make sure the queue isnt empty
string empty = "The queue is empty";
return empty;
}else{
Job remove = array[1];
array[1] = array[count--];
int j;
Job v;
int k = 1;
v = array[k];
while(k <= count/2){
cout << "dequeuewhile"; // test
j = k + k;
if(j < count && array[j].getPriority() > array[j+1].getPriority()){
j++;
cout << "dequeueloop if1"; // test
}
if(v.getPriority() <= array[j].getPriority()){
cout << "dequeueloop if2"; //test
break;
}
array[k] = array[j];
k = j;
}
array[k] = v;
return remove.getTaskName(); // returns the name of the removed job
}
}
string PriorityQueue::peek() { // returns the name of the highest priority job without removing it from the queue
if(count == 0){
return array[0].getTaskName();
}
return array[1].getTaskName();
}
int PriorityQueue::peekPriority() { // returns the priority from the highest priority job without removing it from the queue
if(count == 0){
cout << "\tThere are no items in the list.\n";
return array[0].getPriority();
}
return array[1].getPriority();
}
【问题讨论】:
-
请发帖minimal reproducible example。就目前而言,您的
expandCapacity有几个缺陷,一个是您在调用new[]之前更改了成员变量(capacity)。如果new[]抛出异常,您的PriorityQueue对象将被损坏。另一个问题是您的PriorityQueue类不遵循 3 的规则。如果在程序中的任何位置复制了PriorityQueue,则行为未定义。为什么不直接使用std::vector<Job> array而不是Job* array? -
我在示例中尽了最大努力,我真的不知道出了什么问题。容量发生变化,因此用 2 倍大小的新阵列替换旧阵列。我无法为此使用向量。
-
在调用
expandCapacity之前,您可能已经损坏了内存。你真的应该按照说明做并发布minimal reproducible example。 -
假设
capacity为1,假设您将count初始化为0,因此数组具有capacity元素。自count < capacity以来容量未增加。所以你增加count,然后你尝试访问array[1],但是array[1]超出了范围。因此,除非您向我们展示count的设置,否则就是错误。我知道您将初始容量设置为 5,但将其设置为 1,您应该会看到代码失败的地方。 -
我已经编辑了我希望是一个可以接受的例子。