结构作为参数的函数产生错误:free():无效指针:0x00007efd47b



>我做了一个addDepartment函数,它将结构作为参数。当我输入以初始化函数底部的"dept[counter].DepartmentHead"时,它会触发错误消息。

正在从我使用类而不是结构编写的另一个代码中复制逻辑,并且该代码工作正常,所以我真的不确定为什么这个不起作用。尝试弄乱索引以确保我没有超过数组的大小,但这似乎不能解决问题。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
struct Department{
    string departmentName;
    string departmentHead;
    int departmentID;
    double departmentSalary;
};
//...
Department addDepartment(Department dept[3]){
    int repeat=0;
    int counter=0;
    if (counter>2){
        cout<<"The array is full, you can not add any more Departments."<<endl;
    }
    else{
        cout << "Please Enter Department Details:"<<endl;
        cout << "Department ID : ";
        cin >> dept[counter].departmentID;
        for(int x=0; x<3; x++){
            for (int y=x+1; y<3; y++){
                if(dept[x].departmentID==dept[y].departmentID)
                    repeat++;
            }
        }
        if(repeat!=0)
            cout<<"Value must be unique!"<<endl;
        else{
            cout << "Department Name : ";
            cin >> dept[counter].departmentName;
            cout << "Head of Department : ";
            cin >> dept[counter].departmentHead;
            counter++;
        }
    }
}
//...
int main()
{
    Employee emp[5];
    Department dept[3];
    initialID(emp,dept,0);
    initialID(emp,dept,1);
    int response;
    while(response!=6){
        displayMenu();
        cout<< "Please make a selection : n";
        cin >> response;
        while((response!=1)&&(response!=2)&&(response!=3)&&(response!=4)&&(response!=5)&&(response!=6)){
            cout<< "Please enter a valid choice (1 - 6): ";
            cin >> response;
        }
        if(response==1){
            addDepartment(dept);
        }
        else if(response==2){
            //addEmployee(emp,dept);
        }
        else if(response==3){
        }
        else if(response==4){
        }
        else if(response==5){
            //salaryReport(dept);
        }
    }
        cout << "Thank you, goodbye.";  
}

为什么会中断。addDepartment函数实际上永远不会返回部门。当函数退出时,返回新创建的部门所在的空间将保持未初始化状态。这会导致未定义的行为。编译器尝试像往常一样销毁Department对象,但由于它从未初始化过,因此free垃圾被调用(导致错误(。

我们可以通过在返回实际部门addDepartment添加一行来解决此问题:

Department addDepartment(Department dept[3]){
    int repeat=0;
    int counter=0;
    if (counter>2){
        cout<<"The array is full, you can not add any more Departments."<<endl;
    }
    else{
        cout << "Please Enter Department Details:"<<endl;
        cout << "Department ID : ";
        cin >> dept[counter].departmentID;
        for(int x=0; x<3; x++){
            for (int y=x+1; y<3; y++){
                if(dept[x].departmentID==dept[y].departmentID)
                    repeat++;
            }
        }
        if(repeat!=0)
            cout<<"Value must be unique!"<<endl;
        else{
            cout << "Department Name : ";
            cin >> dept[counter].departmentName;
            cout << "Head of Department : ";
            cin >> dept[counter].departmentHead;
            counter++;
        }
    }
    return /* some department */;
}

或者,您可以使addDepartment无效。

其他注意事项。不要将原始 C 数组传递给函数。它不会按照您的意图执行。

如果要传递数组的副本,请传递一个std::array,该将自动复制:

Department addDepartment(std::array<Department, 3> dept); 

如果要访问现有数组的元素,请传递一个指针:

Department addDepartment(Department* dept, int count); 

我看到的一个问题是,您正在main中创建 3 个Department对象的数组,并假设您在initialID中有 5 个元素。

更改main以创建包含 5 个Department对象的数组。

int main()
{
    Employee emp[5];
    Department dept[5];
    ...

最新更新