c++动态数组结构体分割错误

C++ Dynamic array of structs segmentation fault

本文关键字:分割 错误 结构体 数组 动态 c++      更新时间:2023-10-16

我正在编写一个程序,该程序从名为"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元素大小的新添加的联系人。

正如在评论中提到的,你的文件结束检查是错误的,这个和这个问题是相关的