深度复制的分段错误
segmentation fault with deep copy
我试图对对象进行深度复制(用于写入时的复制),但遇到了分段错误。
我正在使用一个带有链表的哈希表。
class Person
{
public:
Person(const char * id,int nb)
{
this->id=strdup(id);
this->nb=nb;
this->init=init;
this->next=NULL;
}
Person(const Person& rhs) :
nb(rhs.nb),
init(rhs.init),
id(strdup(rhs.id)),
next(rhs.next == NULL ? NULL : new Person(*rhs.next)) {}
char* strdup(char const* in)
{
char* ret = new char[strlen(in)+1];
strcpy(ret, in);
return ret;
}
int nb,init;
const char * id;
Person *next;
};
Hashtable deepcopy (const Hashtable& rhs)
{
num[0]=num[0]-1;
Person** array=rhs.table;
Hashtable autre;
for (int i = 0 ; i < size; ++i)
if (autre.table[i]!=NULL)
autre.table[i] = new Person(*array[i]);
return autre;
num[0]=1;
}
我的类的属性Hashtable:
Person **table;
int* num;
编辑:这个问题似乎已经解决了我的深度复制有什么问题?我不明白。我认为我的复制构造函数很好,但我不明白为什么我在运行它时会出现seg错误。
必须修复此代码:
for (int i = 0 ; i < size; ++i)
autre.table[i] = new Person(*array[i]);
table
具有固定的大小,并且它填充了空指针。在循环中,您不会检查要复制的元素是否为空指针,因此您可以取消对它的隔离,并尝试复制甚至不存在的实体。
for (int i = 0 ; i < size; ++i) {
if(array[i] != NULL) {
autre.table[i] = new Person(*array[i]);
}
}
附言:最好使用nullptr
而不是NULL
。
我看到的问题:
-
Person
的默认构造函数。Person(const char * id,int nb) { this->id=id; this->next=NULL; }
如果我使用
Person foo() { char id[] = "John"; return Person(id, 0); } Person a = foo();
然后,用于在
foo
中保存"John"的堆栈内存现在被a
保留,这将导致未定义的行为。您需要获得输入字符串的所有权。将
std::string
用于id
,而不是char const*
。 -
复制
Person
的构造函数。声明
id(rhs.id),
如果您决定使用
char const*
作为id
的类型,将是一个问题。如果您将其切换到std::string
,则不会有问题。 -
HashTable
的复制构造函数生成table
的浅复制。如果您决定删除HashTable
的析构函数中的table
,这将是一个问题。如果不在HashTable
的析构函数中删除table
,则表示内存泄漏。 -
在
deepcopy
中,在取消引用array[i]
之前,您不会检查它是否为NULL。@alphabhoter已经指出了这一点。此外,您正在函数的局部变量autre
中创建一个深度副本。除非您从函数中返回autre
,否则深度副本在函数外不可见。
编辑由于不允许使用std::string
,因此必须在默认构造函数和复制构造函数中为char const*
分配内存。如果您的平台具有非标准函数strdup
,并且您可以使用它,则可以将默认构造函数更改为:
Person(const char * id,int nb)
{
this->id=strdup(id);
this->next=NULL;
}
您需要对复制构造函数进行类似的更改。
如果你没有strdup
或者不允许使用它,你可以定义它。这是一个非常简单的函数。
char* strdup(char const* in)
{
char* ret = new char[strlen(in)+1];
strcpy(ret, in);
return ret;
}
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)