C ++动态数组结构分段错误

时间:2015-10-14 12:22:44

标签: c++ arrays

我正在开发一个程序,该程序从名为" phonebook.txt"的文本文件中读取数据。并在终端显示。它询问用户想要添加多少联系人。用户将输入他/她想要的联系人数量,程序将与来自" phonebook.txt"的旧联系人一起输出该联系人。到"更新-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; 
}

我正在尝试将用户创建的新联系人和读取文件中的旧联系人存储在一个动态结构数组中。我得到这个分段错误是因为我使用了两个不同的索引&#34; count&#34;和&#34;索引&#34;将联系人存储在同一动态数组中?

1 个答案:

答案 0 :(得分:2)

您的segfault正在发生,因为您正在尝试访问contact数组的越界元素。 contact的大小为amount,您将其从0迭代到amount + count。显然,amount + count >= amount,迟早会超出界限。

我建议您使用std::vector而不是普通数组。你将始终意识到它的大小,并能够安全地迭代它。

如果要保留数组,则必须在从旧文件复制联系人后重新分配contact,使其大小等于total,或者创建两个单独的数组:保存来自旧文件的记录,其大小为amount元素,另一个记录用于新添加的count元素大小的联系人。

正如评论中提到的那样,您的文件结束检查有误,{​​{3}}和this问题是相关的。