gpt4 book ai didi

C++ 中止核心在执行结束时转储

转载 作者:行者123 更新时间:2023-11-30 04:50:33 26 4
gpt4 key购买 nike

我是 C++ 的新手,目前正在做一项关于 vector 的学校作业。头文件包含满足要求所需的函数的实现。我即将完成该程序,但不幸的是,在执行结束时,它给出了“中止核心转储”消息。我似乎无法弄清楚为什么会这样。一切都运行良好,直到结束。它是在分配测试期间发出该消息的。我需要你的专业知识。谢谢!

我的输出:

********** TestAssign **********
push_back 10 floats:
0 2 4 6 8 10 12 14 16 18 (size=10 capacity=16)
0 1 2 3 4 5 6 7 8 9 (size=10 capacity=16)
Assign: b = a, print a,b
0 2 4 6 8 10 12 14 16 18 (size=10 capacity=16)
0 2 4 6 8 10 12 14 16 18 (size=10 capacity=16)
Assign: a = a, print a
0 2 4 6 8 10 12 14 16 18 (size=10 capacity=16)
Aborted (core dumped)

学校的输出(正确的一个):

********** TestAssign **********
push_back 10 floats:
0 2 4 6 8 10 12 14 16 18 (size=10 capacity=16)
0 1 2 3 4 5 6 7 8 9 (size=10 capacity=16)
Assign: b = a, print a,b
0 2 4 6 8 10 12 14 16 18 (size=10 capacity=16)
0 2 4 6 8 10 12 14 16 18 (size=10 capacity=16)
Assign: a = a, print a
0 2 4 6 8 10 12 14 16 18 (size=10 capacity=16)

头文件(我的实现):

template <typename T>
vec
{

private:
T* v;
int count;
int capacity;
public:

vector(){ //default constructor
capacity=0;
v = nullptr;
count = 0;
}

vector(const vector& b) //copy contructor
{
count = b.count;
capacity = b.capacity;
v = new T[capacity];
for (int i = 0; i < capacity; i++)
{
this->v[i] = b.v[i];
}
}

vector(vector&& tmp): v(tmp.v),//move constructor
count(tmp.count),
capacity(tmp.capacity)
{
tmp.v = nullptr;
}

~vector() {//destructor

if (v != NULL);
{
delete [] v;
v = NULL;
}
}

void push_back(const T& t)
{
if(count+1>capacity)
{
capacity = std::max(2*capacity, 1);

T* newData = new T[capacity];
for(int i=0; i <count; i++)
{
newData[i] = v[i];
}
delete[] v;
v = newData;
}
v[count++] = t;
}

T pop_back() {
//..
}

size_t size() const
{
//..
}

bool empty()
{
//..
}

T operator[](unsigned index) const //subscript operator
{
//...
}

T& operator[](unsigned index) //subscript operator
{
//...
}

void clear(){
//...
}

void erase(T position)
{
//...
}

void insert(int index, T number)
{
//...
}

vector& operator = (const vector &rhs) //copy assignment
{
../
}

vector& operator=(vector&& rhs) { //move assignment

if (this != &rhs) {
delete[] v;
this->v = rhs.v;
rhs.v = nullptr;
}
return *this;
}

template <typename T1>
friend void Print(const vector<T1>& s); //friend print

friend void Print(const vector<unsigned char>& s); //friend print if
//unsigned

};
template <typename T1>
void Print(const vector<T1>& s)
{
// std::cout << std::fixed << std::setprecision(2);
for(int i = 0; i < s.count; i++)
{
std::cout<<s.v[i]<<" ";
}
std::cout<< "(size=" << s.count << " " << "capacity=" << s.capacity <<
")";
std::cout<<std::endl;
}

void Print(const vector<unsigned char>& s)
{
//...
}

主文件:

void TestAssign(void)
{
std::cout << "\n********** TestAssign **********\n";
cs150::vector<float> a, b;

std::cout << "push_back 10 floats:\n";
for (float i = 0; i < 10; i++) {
a.push_back(2 * i);
b.push_back(i);
}

Print(a);
Print(b);

std::cout << "Assign: b = a, print a,b\n";
b = a;
Print(a);
Print(b);

std::cout << "Assign: a = a, print a\n";
a = a;
Print(a);
}

最佳答案

问题出在你的赋值运算符上:

vector& operator = (const vector &rhs) //copy assignment
{
v = rhs.v;
return *this;
}

复制指针 v 而不是复制底层数组(您还忘记复制其他成员,countcapacity)

因此,在 b = a; 之后,您最终会在 ab 中双重释放相同的数组,这会导致段错误。

将赋值运算符更改为更像复制构造函数:

vector& operator = (const vector &b) //copy assignment
{
if (this != &b) {
count = b.count;
capacity = b.capacity;
v = new T[capacity];
for (int i = 0; i < count; i++)
{
this->v[i] = b.v[i];
}
}
return *this;
}

不相关的注释:你在析构函数中有一个额外的;:

    if (v != NULL);
^ here

除非您需要支持它,否则我现在也会将移动语义从中移除。

关于C++ 中止核心在执行结束时转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54959253/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com