c++ - 线程 1:EXC_BAD_ACCESS (code=1, address=0x0) 在尝试 mergeSort 时崩溃
问题描述
嗨所以实际上有2个问题。通过此函数的所有路径都将自称是什么意思?此外,我在放置错误的地方遇到了分段错误。我尝试调试,但我很困惑为什么它不起作用。如果有人不仅可以为我做这件事,而且还可以帮助解释我做错了什么,那将有很大帮助。
bool operator<=(const Record& rightObj) const {
return this->k <= rightObj.k;
}
template <typename T>
class LinkedList : public List<T> {
protected:
// represents an element in the linked list
struct Node {
T value;
Node* next;
Node(T v, Node* n = nullptr)
: value(v), next(n) { }
};
// a pointer to the front of the list
Node* head;
private:
// copy the values from the argument linked list to `this`
void copy(const LinkedList<T>&);
// merge sort algorithm
void mergeSort(Node*&);
void split(Node*, Node*&, Node*&);
void merge(Node*&, Node*, Node*);
public:
// default constructor
LinkedList();
// copy constructor
LinkedList(const LinkedList<T>&);
// overloaded assignment operator
LinkedList<T>& operator=(const LinkedList<T>&);
// destructor
virtual ~LinkedList();
// add the argument to the end of the list
virtual void append(const T&) override;
// remove all elements in the list
virtual void clear() override;
// return the element at the given position (argument)
virtual T getElement(int) const override;
// return the current length of the list
virtual int getLength() const override;
// insert the given element (argument 2) at
// the given position (argument 1)
virtual void insert(int, const T&) override;
// determine if the list currently empty
virtual bool isEmpty() const override;
// print the elements in the list starting at the given Node (argument)
void print(Node*) const;
// remove the element at the given position (argument)
virtual void remove(int) override;
// replace the element at the given position (argument 1) with
// the value given (argument 2)
virtual void replace(int, const T&) override;
// sort the elements in the list using the algorithm (argument)
// 4 --> merge sort
virtual void sort(int) override;
};
template <typename T>
void LinkedList<T>::mergeSort(Node*& curr) {
Node* n = curr;
Node* a;
Node* b;
if (n == nullptr || n->next == nullptr) {
return;
} // if
// splits linked list
split(n, a, b);
// recursive calls for sorting
mergeSort(a);
mergeSort(b);
// merging lists
merge(n, a, b);
}
template <typename T>
void LinkedList<T>::merge(Node*& curr, Node* A, Node* B) { //I get "All paths through this function will call itself here"
// Node* result = nullptr;
if (A == nullptr) {
curr = B;
} // if
else if (B == nullptr) {
curr = A;
} // else if
if (A->value <= B->value) { //Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
// cout << "TEST" << endl;
// result = A;
cout << "TEST" << endl;
merge(curr, A->next, B); // Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
cout << "TEST" << endl;
} // if
else {
// cout << "TEST" << endl;
// result = B;
merge(curr, A, B->next); // segmenation fault here
} // else
curr = A;
}
解决方案
推荐阅读
- python - 为什么 scikit-learn r2 得分为零?
- python - Heroku 通过外部 SMTP 服务器发送邮件
- python - 是否有可能在字典中查找字符串?
- drupal - 在没有编辑权限的情况下翻译内容时访问菜单设置
- java - 用于容器化应用程序的 Open JDK 和 Oracle JDK 之间的区别
- java - 我的 Spring Boot 项目有映射异常
- javascript - 在 React 中将数组转换为组件
- html - 在悬停时显示包含整个文本的吐司
- python - 使用Openpyxl将IF公式插入Excel后出现“@”符号
- python - Django 外键 JWT 身份验证