我遇到问题,当我尝试对已制成的缓冲区进行排序时,最终收到错误消息:线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x7)。我已经尝试过,但是到目前为止,我一直未能找到确切的问题所在,并一直认为我可能需要一些新鲜的新眼睛来看清我所缺少的东西。
main.cpp
#include "int_sorted.hpp"
#include <cstdlib>
void fill(int_buffer &buffer);
int main(int argc, const char * argv[]) {
int_buffer buffer(10);
fill(buffer);
int_sorted sort(buffer.begin(), buffer.size());
sort.print();
return 0;
}
void fill(int_buffer &buffer){
int value = 0;
for(int *i = buffer.begin(); i !=buffer.end(); i++){
value = rand()%10;
*i = value;
}
for(const int* i = buffer.begin(); i != buffer.end(); i++){
std::cout << *i << std::endl;
}
}
缓冲区代码。
#include "int_buffer.hpp"
#include <algorithm>
int_buffer::int_buffer(size_t size):buffer(new int[size]), bufferSize(size){
}
int_buffer::int_buffer(const int* source , size_t size):buffer(new int[size]), bufferSize(size){
for(int i = 0; i < size; i++){
buffer[i] = source[i];
}
}
int_buffer::int_buffer(const int_buffer& rhs):buffer(new int[rhs.size()]), bufferSize(rhs.size()){
std::copy(rhs.begin(), rhs.end(), begin());
}
int_buffer::int_buffer(int_buffer&& rhs){
buffer = rhs.buffer;
bufferSize = rhs.size();
rhs.buffer = nullptr;
}
// ---- New edit ----
int_buffer& int_buffer::operator=(const int_buffer& rhs){
std::cout << "Construct copy assigned." << std::endl;
int_buffer cpy_asgn(rhs);
swap(cpy_asgn);
return *this;
/*
if(buffer == rhs.begin()){
return *this;
}else if(bufferSize == rhs.size()){
std::copy(rhs.begin(), rhs.end(), begin());
return *this;
}else{
delete []buffer;
buffer = new int[rhs.size()];
bufferSize = rhs.size();
std::copy(rhs.begin(), rhs.end(), begin());
return *this;
}
*/
}
int_buffer& int_buffer::operator=(int_buffer&& rhs){
if(this != &rhs){
delete []buffer;
this->buffer = rhs.buffer;
rhs.buffer = nullptr;
}
return *this;
}
size_t int_buffer::size() const{
return bufferSize;
}
int* int_buffer::begin(){
return buffer;
}
int* int_buffer::end(){
return buffer + bufferSize;
}
const int* int_buffer::begin() const{
return buffer;
}
const int* int_buffer::end() const{
return buffer + bufferSize;
}
int_buffer::~int_buffer(){
delete []buffer;
}
// ---- New edit ----
void int_buffer::swap(int_buffer& rhs){
std::swap(this->buffer, rhs.buffer);
std::swap(this->bufferSize, rhs.bufferSize);
}
排序代码。
#include "int_sorted.hpp"
int* ins_pos = nullptr;
int_sorted::int_sorted(const int* source , size_t size):buffer(source, size){
/*if(!sort_tested()){
*this = sort(begin(), end());
}*/
*this = sort(begin(), end());
}
int_sorted::int_sorted():buffer(nullptr, 0){}
size_t int_sorted::size() const{
return buffer.size();
}
int* int_sorted::insert(int value){
int_buffer buffer(&value, 1);
if(!sort_tested()){
sort(begin(), end());
}
*this = merge(int_sorted(buffer.begin(), 1));
return ins_pos;
}
const int* int_sorted::begin() const{
return buffer.begin();
}
const int* int_sorted::end() const{
return buffer.end();
}
bool int_sorted::sort_tested(){
int* a = buffer.end();
int* b = nullptr;
while(a != buffer.end()){
b = a + 1;
if(b == buffer.end()){
return true;
}
if(*a > *b){
return false;
}
a = b;
}
return true;
}
int_sorted int_sorted::merge(const int_sorted& merge_with) const{
int_buffer tempBuffer(buffer.size() + merge_with.size());
const int* x = buffer.begin();
const int* y = merge_with.begin();
int* z = tempBuffer.begin();
while(x != buffer.end() && y != merge_with.end()){
if(*x < *y){
*z = *x;
x++;
}else{
*z = *y;
y++;
if(merge_with.size() == 1){
ins_pos = z;
}
}
z++;
}
while(x != buffer.end()){
*z = *x;
x++;
z++;
}
while(y != merge_with.end()){
*z = *y;
y++;
z++;
if(merge_with.size() == 1){
ins_pos = z;
}
}
return int_sorted(tempBuffer.begin(), tempBuffer.size());
}
int_sorted int_sorted::sort(const int* begin , const int* end){
if ( begin == end )
return int_sorted();
if ( begin == end - 1 )
return int_sorted(begin, 1);
ptrdiff_t half = ( end-begin )/2; // pointer diff type
const int* mid = begin + half;
return sort(begin , mid).merge(sort(mid, end));
}
void int_sorted::print(){
for(auto i = buffer.begin(); i != buffer.end(); i++)
std::cout << *i << std::endl;
}
缓冲区头。
#ifndef int_buffer_hpp
#define int_buffer_hpp
#include <stdio.h>
#include <iostream>
class int_buffer{
public:
explicit int_buffer(size_t size); // size_t is defined in cstdlib
int_buffer(const int* source , size_t size);
int_buffer(const int_buffer& rhs); // copy construct
int_buffer(int_buffer&& rhs); // move construct
int_buffer & operator=(const int_buffer& rhs); // copy assign
int_buffer & operator=(int_buffer&& rhs); // move assign
size_t size() const;
int* begin();
int* end();
const int* begin() const;
const int* end() const;
~int_buffer();
private:
// ---- New edit ----
void swap(int_buffer& rhs);
int* buffer;
size_t bufferSize;
};
#endif /* int_buffer_hpp */
排序标题。
#ifndef int_sorted_hpp
#define int_sorted_hpp
#include <stdio.h>
#include "int_buffer.hpp"
class int_sorted{
public:
int_sorted(const int* source , size_t size);
int_sorted();
size_t size() const;
int* insert(int value); // returns the insertion point.
const int* begin() const;
const int* end() const;
bool sort_tested();
int_sorted merge(const int_sorted& merge_with) const;
int_sorted sort(const int* begin , const int* end);
void print();
private:
int_buffer buffer;
};
#endif /* int_sorted_hpp */