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

Program crashes when deleting dynamic array C++

本文关键字:程序 崩溃 C++ 动态 数组 删除      更新时间:2023-10-16

我写了一个程序,为结构数组动态分配内存。它似乎工作正常,但是当我尝试删除数组时,计算机会发出"砰"声,程序停止响应。它不会抛出错误或任何东西,它只是停止。我尝试了deletedelete[],结果相同。我尝试移动删除的位置,发现我可以在创建后立即删除它,但不能在它传递给任何函数后删除它。谁能告诉我为什么我不能删除内存?

#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] = { '' };
    //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] = '';
        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. 不要delete work_force; PrintData的顶部.

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

  3. 使用std::vector<Employee> . 重要的是,这意味着您不再需要使用new(这是您应该始终尝试避免的事情)。 这也意味着您只需要读取文件一次 - 您只需读取条目,然后用 push_back 将其推送到向量上。

  4. 从输入流读取时,您需要尝试读取然后测试流以查看是否已命中 eof。 因此,读取函数如下所示:

        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);
        }