c++动态数组结构体分割错误
C++ Dynamic array of structs segmentation fault
我正在编写一个程序,该程序从名为"phonebook.txt"的文本文件中读取数据并将其显示在终端中。它询问用户想要添加多少联系人。用户将输入他/她想要的联系人数量,程序将输出这个和旧联系人一起从"phonebook.txt"到"updated-phonebook.txt"。现在我选择使用动态数组而不是矢量,因为我想感受一下如何处理内存分配。
#include <iostream>
#include <string>
#include <fstream>
#define nullptr 0;
struct Person
{
std::string lName, fName, streetAddr, city, state, zip, phoneNum;
};
int main ()
{
std::ofstream outFile;
std::string dummy;
int amount, total, count = 0;
Person *contact;
//set the pointer to 0 so its not pointing
//to anything else in memory
contact = nullptr;
std::ifstream inFile("/home/isemanthaj/My_Programs/Class_Programs/Assignment_3/phonebook/phonebook.txt",std::ios::in);
if (!inFile)
{
std::cout << "File failed to open" << std::endl;
}
else
outFile.open("updated_phonebook.txt", std::ios::out);
std::cout << "How many contacts do you want to add?" << std::endl;
std::cin >> amount;
contact = new Person[amount];
std::cout << "Contacts in the previous book n" << std::endl;
std::cin.ignore();
while (inFile)
{
//dummy stores the contact number which is the first line
// in the read file. I don't want to carry the number over
// to the other updated_phonebook.txt file
std::getline(inFile, dummy);
std::getline(inFile, contact[count].lName);
std::getline(inFile, contact[count].fName);
std::getline(inFile, contact[count].streetAddr);
std::getline(inFile, contact[count].city);
std::getline(inFile, contact[count].state);
std::getline(inFile, contact[count].zip);
std::getline(inFile, contact[count].phoneNum);
std::cout << contact[count].lName << std::endl;
std::cout << contact[count].fName << std::endl;
std::cout << contact[count].streetAddr << std::endl;
std::cout << contact[count].city << std::endl;
std::cout << contact[count].state << std::endl;
std::cout << contact[count].zip << std::endl;
std::cout << contact[count].phoneNum << std::endl;
outFile << contact[count].lName << std::endl;
outFile << contact[count].fName << std::endl;
outFile << contact[count].streetAddr << std::endl;
outFile << contact[count].city << std::endl;
outFile << contact[count].state << std::endl;
outFile << contact[count].zip << std::endl;
outFile << contact[count].phoneNum << std::endl;
count++;
}
// I know this is a little wacky here.
// I want the program to display the total amount of contacts
// to the screen
total = amount + count;
for (int index = 0; index < total; index++)
{
std::cout << "Last name: ";
std::getline(std::cin, contact[index].lName);
std::cout << "First name: ";
std::getline(std::cin, contact[index].fName);
std::cout << "Street address: ";
std::getline(std::cin, contact[index].streetAddr);
std::cout << "City: ";
std::getline(std::cin, contact[index].city);
std::cout << "State: ";
std::getline(std::cin, contact[index].state);
std::cout << "Zip code: ";
std::getline(std::cin, contact[index].zip);
std::cout << "Phone number: ";
std::getline(std::cin, contact[index].phoneNum);
std::cout << std::endl;
std::cout << "Contact: " << index + 1 << std::endl;
std::cout << "Last name: " << contact[index].lName << std::endl;
std::cout << "First name: " << contact[index].fName << std::endl;
std::cout << "Street address: " << contact[index].streetAddr << std::endl;
std::cout << "City: " << contact[index].city << std::endl;
std::cout << "State: " << contact[index].state << std::endl;
std::cout << "Zip code: " << contact[index].zip << std::endl;
std::cout << "Phone number: " << contact[index].phoneNum << std::endl;
std::cout << std::endl;
outFile << "Last name: " << contact[index].lName << std::endl;
outFile << "First name: " << contact[index].fName << std::endl;
outFile << "Street address: " << contact[index].streetAddr << std::endl;
outFile << "City: " << contact[index].city << std::endl;
outFile << "State: " << contact[index].state << std::endl;
outFile << "Zip code: " << contact[index].zip << std::endl;
outFile << "Phone number: " << contact[index].phoneNum << std::endl;
}
inFile.close();
outFile.close();
delete [] contact;
return 0;
}
我试图将用户创建的新联系人和从读取文件中的旧联系人存储在一个动态结构数组中。我得到这个分割错误,因为我使用两个不同的索引"计数"answers"索引"存储在同一个动态数组中的联系人?
您的段错误正在发生,因为您试图访问contact
数组的越界元素。contact
的大小是amount
,从0迭代到amount + count
。显然,amount + count >= amount
,所以你迟早会跑出边界。
我建议您使用std::vector
而不是普通数组。你将始终知道它的大小,并且能够安全地迭代它。
如果你想保留数组,你必须在从旧文件复制联系人后重新分配contact
,使其大小等于total
,或者创建两个单独的数组:一个保存旧文件中amount
元素大小的记录,另一个保存count
元素大小的新添加的联系人。
正如在评论中提到的,你的文件结束检查是错误的,这个和这个问题是相关的
相关文章:
- C++映射分割错误(核心转储)
- 由cin中的字符串中未捕获空白引起的分割错误
- 删除映射和分割错误中的一个过去结束元素
- 在指向函数中读取变量时出现分割错误
- 在链表中的第 n 位插入显示分割错误
- 较高值 n 的分割错误(例如 n=999997)
- 尝试通过memcpy复制大尺寸浮点向量时的分割错误
- 分割错误:向量中的擦除功能
- 向量向量的分割错误
- 我在C++中编写了一个方法来打印树类的预序,但它显示了分割错误
- C ++分割错误,为什么使用"long long"我没有得到答案?
- 在尝试使用递归查找集合子集的总数时,我遇到了分割错误
- 分割错误 11:尝试使用 cin 输入 B[1] 时
- 集合布局上的 Qt 分割错误
- 高达20亿的筛子会产生分割错误
- 对向量使用推回函数时的分割错误
- 绘制精灵会导致分割错误
- 将矢量的整数内容打印为字符串会导致分割错误
- 分割错误:使用向量时为 11 c++
- 结构的分割错误错误