gpt4 book ai didi

c++ - 使用递归方法遇到段错误

转载 作者:行者123 更新时间:2023-11-30 05:02:20 27 4
gpt4 key购买 nike

class Apple
{
public:
Apple func(int depth) const;
private:
int size = 0; // size of data array
double *data = nullptr;
}

Apple::func(int depth) const
{
Apple new_apple;

'''
some calculations
'''

if(depth>1)
{
return new_apple.func(depth-1);
}else
{
return new_apple;
}
}

在方法 func() 中,我创建了一个新的 Apple 对象,并根据存储在原始 Apple 对象中的数据进行了一些计算。终端给出

Segmentation fault (core dumped)

当我从 valgrind 收到以下消息时,我认为内存有问题。

==4787== HEAP SUMMARY:
==4787== in use at exit: 5,994 bytes in 63 blocks
==4787== total heap usage: 1,059 allocs, 996 frees, 146,564 bytes allocated
==4787==
==4787== LEAK SUMMARY:
==4787== definitely lost: 0 bytes in 0 blocks
==4787== indirectly lost: 0 bytes in 0 blocks
==4787== possibly lost: 0 bytes in 0 blocks
==4787== still reachable: 5,994 bytes in 63 blocks
==4787== of which reachable via heuristic:
==4787== newarray : 1,080 bytes in 3 blocks
==4787== suppressed: 0 bytes in 0 blocks

Valgrind 跟踪到 Apple 类的重载 operator=。只有一个成员(我这里命名为data)涉及到动态内存,所以这里只展示和它相关的部分。以下是 operator= 的实现方式:

Apple& Apple::operator=(const Apple &obj)
{
if(this!=&obj)
{
if(this->data)
{
delete[] this->data;
}
this->size = obj.size;
this->data = new double[this->size];
for(int i=0; i<this->size; i++)
{
this->data[i] = obj.data[i];
}
}
return *this
}

============================================= ==========================

如果我不添加递归组件,方法 func() 工作正常。而且我也用valgrind查过,计算部分的内存没有问题。

Apple::func(int depth) const
{
Apple new_apple;

'''
some calculations
'''
return new_apple; // works fine
}

即使我调用 depth = 1 的 func(int depth) 也会发生相同的段错误。

Apple::func(int depth) const
{
Apple new_apple;

'''
some calculations
'''

if(depth>1)
{
std::cout << 1 << std::endl;
return new_apple.func(depth-1);
}else
{
std::cout << 2 << std::endl;
return new_apple;
}
}

当我调用 func(1) 时,既没有打印出“1”也没有打印出“2”。

============================================= ==========================来自 gdb 的调试信息:

Program received signal SIGSEGV, Segmentation fault.
Apple::operator= (this=0x0, T=...) at src/xxxx.cpp:1011
1011 if(this->data)

============================================= ==========================更新:(添加了导致错误的示例代码)

我添加了一个示例代码,它更接近我的真实代码的结构。

示例代码:

http://coliru.stacked-crooked.com/a/3f7e50e4ebabf717

#include <iostream>
using namespace std;
class Apple
{
public:
Apple();
~Apple();
Apple(const Apple &obj);
void alloc(int size);
private:
double *data = nullptr;
int size = 0;
};
/////////////////////////////////////////////
class Basket
{
public:
Basket();
~Basket();
Basket(const Basket &obj);
void alloc(int size1, int size2);
Basket func(int depth);
private:
Apple *data = nullptr;
int size;
};
//////////////////////////////////////////////
Apple::Apple(){}
Apple::~Apple()
{
if(this->data)
{
delete[] this->data;
}
}
Apple::Apple(const Apple &obj)
{
if(this->data)
{
delete[] this->data;
}
this->size = obj.size;
this->data = new double[this->size];
for(int i=0; i<this->size; i++)
{
this->data[i] = obj.data[i];
}
}
void Apple::alloc(int size)
{
this->size = size;
if(this->data)
{
delete[] this->data;
}
this->data = new double[size];
}
///////////////////////////////////////////////////////////////////////
Basket::Basket(){}
Basket::~Basket()
{
if(this->data)
{
delete[] this->data;
}
}
Basket::Basket(const Basket &obj)
{
this->size = obj.size;
// This is the missing piece
/*
if(this->data)
{
delete[] this->data;
}
this->data = new Apple[this->size];
*/
for(int i=0; i<this->size; i++)
{
this->data[i] = obj.data[i];
}
}
void Basket::alloc(int size1, int size2)
{
if(this->data)
{
delete[] this->data;
}
this->size = size1;
this->data = new Apple[this->size];
for(int i=0; i<size1; i++)
{
this->data[i].alloc(size2);
}
}
Basket Basket::func(int depth)
{
Basket new_basket;
new_basket.alloc(5,5);
if(depth>1)
{
cout << 1 << endl;
return new_basket.func(depth-1);
}else
{
cout << 2 << endl;
return new_basket;
}
}
int main()
{
Basket basket;
Basket new_basket = basket.func(1);
return 0;
}

最佳答案

问题已解决。

错误是由于类Basket的拷贝构造函数实现不当造成的。

我原来的复制构造函数:

Basket::Basket(const Basket &obj)
{
this->size = obj.size;
for(int i=0; i<this->size; i++)
{
this->data[i] = obj.data[i];
}
}

正确的拷贝构造函数:

Basket::Basket(const Basket &obj)
{
this->size = obj.size;
if(this->data)
{
delete[] this->data;
}
this->data = new Apple[this->size];
for(int i=0; i<this->size; i++)
{
this->data[i] = obj.data[i];
}
}

关于c++ - 使用递归方法遇到段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934382/

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