So, I'm trying to implement a priority queue using a sorted list. I have my priority queue class inherit from my sorted list class. My sorted list class works fine when I test its functions in the driver, but I am getting "Segmentation Fault 11"
when I try testing out my implementation of the priority queue. I ran my code through the CLion debugger and got the error "EXC_BAD_ACCESS (code=1, address=0x0)"
, which, after some research, seems to be caused by trying to modify data in a null pointer. This is my first time using smart pointers, so I think my issues lie in a misunderstanding with how they're constructed and initialized.
Here's my header file for my SL_PriorityQueue
class:
#ifndef PRIORITY_QUEUE_
#define PRIORITY_QUEUE_
#include "PriorityQueueInterface.h"
#include "LinkedSortedList.h"
#include "PrecondViolatedExcep.h"
#include <memory>
template<class ItemType>
class SL_PriorityQueue : public PriorityQueueInterface<ItemType>
{
private:
std::unique_ptr<LinkedSortedList<ItemType> > slistPtr;
public:
SL_PriorityQueue();
SL_PriorityQueue(const SL_PriorityQueue& pq);
~SL_PriorityQueue();
bool isEmpty() const;
bool enqueue(const ItemType& newEntry);
bool dequeue();
//@throw PrecondViolatedExcept if priority queue is isEmpty
ItemType peekFront() const throw(PrecondViolatedExcep);
};
#endif
Here's the driver I'm using to test my code:
#include "../src/Node.cpp"
#include "../src/LinkedSortedList.cpp"
#include "../src/SL_PriorityQueue.cpp"
#include <iostream>
int main()
{
std::shared_ptr<SL_PriorityQueue<int> > testing (new SL_PriorityQueue<int>());
testing->enqueue(7);
std::cout << testing->peekFront() << std::endl; //I set a break point here, which is where CLion throws the exception
std::cout << testing->dequeue() << std::endl;
std::cout << testing->isEmpty() << std::endl;
return 0;
}
Here's the function from SL_PriorityQueue.cpp that the CLion highlighted after the exception:
template <class ItemType>
bool SL_PriorityQueue<ItemType>::enqueue(const ItemType& newEntry)
{
slistPtr->insertSorted(newEntry);
return true;
}
The error makes me think that when I call insertSorted in the above function, slistPtr
is still null. My constructor for SL_PriorityQueue
is empty since it seems that with smart pointers they manage their own memory such that I don't have to set it equal to a null pointer.
I tried making slistPtr
a shared pointer to see if maybe too many things were pointing to it, but I just received the same error.
Thanks in advance for any help you can provide!
My constructor for SL_PriorityQueue is empty since it seems that with smart pointers they manage their own memory such that I don't have to set it equal to a null pointer.
If I've understood you correctly, you've got a bit mixed up here. Although a unique_ptr will correctly clean up memory it owns, it won't allocate anything itself. The best way of doing so is to use std::make_unique, which you can call from your constructor.
template <typename ItemType>
SL_PriorityQueue<ItemType>::SL_PriorityQueue()
: slistPtr(std::make_unique<LinkedSortedList<ItemType>>())
{
}
Hope that helps!
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加