删除动态数组C++时程序崩溃
Program crashes when deleting dynamic array C++
我写了一个程序,为结构数组动态分配内存。它似乎工作正常,但是当我尝试删除数组时,计算机会发出"砰"声,程序停止响应。它不会抛出错误或任何东西,它只是停止。我尝试了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] = { ' ' };
//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;
}
}
-
不要
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); }
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 试图创建流或fopen时程序崩溃
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- DLL Made with CMake 使程序崩溃
- 程序崩溃使用boost::asio
- 调用 free() 有时会导致程序崩溃
- 邮件加密程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- For 循环在尝试读取数组 c++ 时程序崩溃
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- Windows桌面程序保存您的计算机会话 - 基于程序崩溃时的恢复会话
- 无法访问的代码如何导致我的程序崩溃?
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- QCompleter set模型使应用程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 删除SDL_PollEvent时程序崩溃