将任意对象附加到数组有什么问题? [英] What is the problem with appending arbitrary object to the array?
问题描述
我有一个简单的程序,可以将文件中的字符串处理为任意类 Student(如下提供)并将其附加到自写容器 ArraySequence.当错误几乎总是发生在标记行(用 ***** 标记)时.
I have a simple program that process strings from a file into arbitrary class Student (provided below) and appends it to self-written container ArraySequence. When error almost always occurs on marked line (marked with *****).
班级学生:
class Student {
public:
int year;
std::string name;
std::string surname;
Student(int _year, std::string _name, std::string _surname) {
this->year = _year;
this->name = _name;
this->surname = _surname;
}
Student(const Student& s) {
this->year = s.year;
this->name = s.name;
this->surname = s.surname;
}
Student& operator=(const Student& s) {
this->year = s.year;
this->name = s.name;
this->surname = s.surname;
return *this;
}
bool operator==(const Student& s) const {
if (this->year == s.year && this->name == s.name && this->surname == s.surname)
return true;
return false;
}
};
主要:
int main() {
std::ifstream data("filename");
std::string buffer;
ArraySequence<Student>* entities = new ArraySequence<Student>;
getline(data, buffer, '\n');
while (buffer.size() != 0) {
std::cout << buffer << std::endl;
int len = static_cast<int>(buffer.find(" "));
std::string surname = buffer.substr(0, len);
buffer = buffer.substr(len + 1);
len = static_cast<int>(buffer.find(" "));
std::string name = buffer.substr(0, len);
int year = std::stoi(buffer.substr(len + 1));
Student b(year, name, surname);
entities->append(b); *****
getline(data, buffer, '\n');
}
std::cout << count << std::endl;
附加:
template <typename T>
void ArraySequence<T>::append(const T& item) {
ISequence<T>::length++;
if (!(ISequence<T>::length - 1)) {
data = (T*)malloc(sizeof(T));
}
else {
data = (T*)realloc(data, sizeof(T) * ISequence<T>::length);
}
*(data + ISequence<T>::length - 1) = item;
}
错误:线程 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
;线程 1: EXC_BAD_ACCESS (code=1, address=0x3)
;
有时它会正常工作,但有时它会因错误而崩溃.
Sometimes it would just work, but other times it crashes with errors.
我使用的是 Xcode 11.1
I'm using Xcode 11.1
在线程 1 中,它说 c++ this->surname = s.surname;
In Thread 1 it says that problem with operator= of Student on the line c++ this->surname = s.surname;
遇到这种情况我该怎么办?有什么想法吗?
What can I do in a situation like this? Any ideas?
推荐答案
T
包含一个非平凡类型(Student
包含 std::string
,加上非平凡的复制/赋值和析构函数),使用 malloc
和 realloc
将无法正常工作.
T
consists of a non-trivial type (Student
contains std::string
, plus non-trivial copy/ assignment and destructor), using malloc
and realloc
will not work correctly.
malloc
和 realloc
不会创建可行的对象.他们只分配内存.由于这些无效的 Student
对象,您的程序可能会崩溃.
malloc
and realloc
do not create viable objects. They only allocate memory. Your program likely crashes due to these invalid Student
objects.
最简单的解决方案是重写ArraySequence
只使用new[]
和delete[]
,而不是malloc
和 realloc
.
The easiest solution is to rewrite ArraySequence<T>::append
to only use new[]
and delete[]
, not malloc
and realloc
.
注意:malloc
可以在使用 时使用place-new,但您没有为此目的使用 malloc
.
Note: malloc
can be used when using placement-new, but you were not using malloc
for this purpose.
这篇关于将任意对象附加到数组有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!