代码之家  ›  专栏  ›  技术社区  ›  Patrick

删除动态数组C时程序崩溃++

  •  -1
  • Patrick  · 技术社区  · 9 年前

    我编写了一个程序,可以为结构数组动态分配内存。它看起来很好,但当我试图删除数组时,计算机会发出“砰”的声音,程序会停止响应。它不会抛出错误或任何东西,只是停止。我试过了 delete delete[] 结果相同。我试过移动delete的位置,发现我可以在它创建后立即删除它,但不能在它传递给任何函数后删除。有人能告诉我为什么我不能删除记忆吗?

    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::ifstream;
    using std::ofstream;
    using std::setw;
    using std::left;
    using std::fixed;
    using std::setprecision;
    
    //structure of an employee
    struct Employee
    {
        char first_name[32];
        char last_name[32];
        char SSN[11];
        float wage;
        int hours;
        char status;
    };
    
    void ReadData(Employee work_force[]);
    void PrintData(Employee work_force[], int number_entries);
    float CalculateStarightPay(Employee worker);
    float CalculateOvertimePay(Employee worker);
    int FindNumberEntries();
    
    const int SPACING = 15;                             //spacing used in formating
    const char dataFile[] = "EmployeeData.txt";         //file to read data from
    const int union_dues = 5;                           //amount deducted from pay for the union
    
    int main()
    {
        int number_entries = FindNumberEntries();
        //array of employees
        Employee * work_force = new Employee[number_entries];
    
        //read in data
        ReadData(work_force);
    
        //prints the data
        PrintData(work_force, number_entries);
    
        //clean up memory
        delete[] work_force;
    
        return 0;
    }
    
    //finds the number of entries in the file
    int FindNumberEntries()
    {
        int counter = 0;
        //worker to read through file entries
        Employee worker_temp;
    
        ifstream input;
    
        input.open(dataFile);
    
        if (input.is_open())
        {
            while (!input.eof())
            {
                input >>
                    worker_temp.first_name >>
                    worker_temp.last_name >>
                    worker_temp.SSN >>
                    worker_temp.wage >>
                    worker_temp.hours >>
                    worker_temp.status;
    
                cin.clear();
    
                counter++;
            }
    
            input.close();
        }
        else
        {
            cout << "File could not be opened!" << endl;
        }
    
            return counter - 1;
    }
    
    //reads employee data from file
    void ReadData(Employee work_force[])
    {
        ifstream input;
    
        input.open(dataFile);
    
        //reads in entries
        if (input.is_open())
        {
            for (int i = 0; !input.eof(); i++)
            {
                //reads in employee data
                input >>
                    work_force[i].first_name >>
                    work_force[i].last_name >>
                    work_force[i].SSN >>
                    work_force[i].wage >>
                    work_force[i].hours >>
                    work_force[i].status;
    
                cin.clear();
            }
    
            input.close();
        }
        else
        {
            //error that file could not open
            cout << "File could not be opened!" << endl;
        }
    
    }
    
    //calculates straight pay
    float CalculateStarightPay(Employee worker)
    {
        //determines of worker is fulltime or not
        if (worker.status == 'F')
        {
            if (worker.hours > 40)
            {
                return ((worker.wage * 40) - 5);
            }
            else
            {
                return (worker.wage * worker.hours) - union_dues;
            }
        }
        else
        {
            if (worker.hours > 40)
            {
                return (worker.wage * 40);
            }
            else
            {
                return worker.wage * worker.hours;
            }
        }
    }
    
    //calculate overtime pay
    float CalculateOvertimePay(Employee worker)
    {
        //deermines if there are any overtime hours
        if (worker.hours <= 40)
        {
            return 0;
        }
        else
        {
            //calculates overtime pay
            return ((worker.hours - 40) * (worker.wage * 1.5));
        }
    }
    
    //prints employee data in a well formated manner
    void PrintData(Employee work_force[], int number_entries)
    {
        delete work_force;
        float straight_pay = 0.0F;
        float Overtime_pay = 0.0F;
        char name[32] = { '\0' };
    
        //print headers
        cout << left <<
            setw(SPACING) << "Name" <<
            setw(SPACING) << "SSN" <<
            setw(SPACING) << "Hourly Wage" <<
            setw(SPACING) << "Hours Worked" <<
            setw(SPACING) << "Straight Pay" <<
            setw(SPACING) << "Overtime Pay" << 
            setw(SPACING) << "Status" << 
            setw(SPACING) << "Net Pay" << endl;
    
        //prints data for each EMPLOYEE
        for (int i = 0; i < number_entries; i++)
        {
            straight_pay = CalculateStarightPay(work_force[i]);
            Overtime_pay = CalculateOvertimePay(work_force[i]);
            //adds a space after first name
            work_force[i].first_name[strlen(work_force[i].first_name) + 1] = '\0';
            work_force[i].first_name[strlen(work_force[i].first_name)] = ' ';
            //puts last name and first name together
            strcpy(name, strcat(work_force[i].first_name, work_force[i].last_name));
    
            //prints out all the data in a nic eformat
            cout << fixed << setprecision(2) <<
                setw(SPACING ) << name <<
                setw(SPACING) << work_force[i].SSN << '$' <<
                setw(SPACING) << work_force[i].wage <<
                setw(SPACING) << work_force[i].hours << '$' <<
                setw(SPACING) << straight_pay << '$' <<
                setw(SPACING) << Overtime_pay <<
                setw(SPACING) << work_force[i].status << '$' <<
                setw(SPACING) << (straight_pay + Overtime_pay) << endl;
        }
    }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Martin Bonner supports Monica    9 年前
    1. 不要 delete work_force; 在顶部 PrintData .

    2. 使用 std::string s代表您的姓名和SSN。(固定长度字符串是一个等待发生的可用性事故)。

    3. 使用 std::vector<Employee> 重要的是,这意味着你不再需要使用 new (这是你应该尽量避免的事情)。这也意味着你只需要读取文件一次-你只需读取条目,然后用 push_back .

    4. 从输入流读取时,您需要尝试读取 然后测试流 看看你是否达到了eof。因此,read函数如下所示:

          while (input >>
               worker_temp.first_name >>
               worker_temp.last_name >>
               worker_temp.SSN >>
               worker_temp.wage >>
               worker_temp.hours >>
               worker_temp.status)
          {
               workforce.push_back(worker_temp);
          }