删除动态数组 C++ 时程序崩溃
Program crashes when deleting dynamic array C++
我编写了一个为结构数组动态分配内存的程序。它似乎工作正常,但每当我尝试删除数组时,计算机都会发出 "bong" 噪音并且程序停止响应。它不会抛出错误或任何东西,它只是停止。我已经尝试了 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] = { '[=10=]' };
//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] = '[=10=]';
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;
}
}
不要delete work_force;
在PrintData
的顶部。
使用 std::string
s 作为您的姓名和 SSN。 (固定长度的字符串是等待发生的可用性事故)。
使用一个std::vector<Employee>
。重要的是,这意味着您不再需要使用 new
(这是您应该始终尝试避免的事情)。这也意味着您只需读取一次文件 - 您只需读取条目,然后将其推送到带有 push_back
.
的向量上
从输入流读取时,需要尝试读取然后测试流看是否命中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);
}
我编写了一个为结构数组动态分配内存的程序。它似乎工作正常,但每当我尝试删除数组时,计算机都会发出 "bong" 噪音并且程序停止响应。它不会抛出错误或任何东西,它只是停止。我已经尝试了 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] = { '[=10=]' };
//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] = '[=10=]';
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;
}
}
不要
delete work_force;
在PrintData
的顶部。使用
std::string
s 作为您的姓名和 SSN。 (固定长度的字符串是等待发生的可用性事故)。使用一个
std::vector<Employee>
。重要的是,这意味着您不再需要使用new
(这是您应该始终尝试避免的事情)。这也意味着您只需读取一次文件 - 您只需读取条目,然后将其推送到带有push_back
. 的向量上
从输入流读取时,需要尝试读取然后测试流看是否命中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); }