在c++中,从结构体中删除成员会产生错误

Deleting a member from a struct gives error in c++

本文关键字:删除成员 错误 结构体 c++      更新时间:2023-10-16

在我的代码中,我有一个这样声明的结构体。

int IN_USE=5;
struct ReadyQueue
{
    int size;
    struct process * Active; // Pointer to another struct 
    struct process readyProcesses[IN_USE]; // contains 5 elements of struct 
                                              process
}ready;

在我的代码中,我正在做删除,ready.readyProcesses [2],

我在运行时得到以下错误

"被释放的指针未被分配"

我有点卡住了,如何克服这个问题。必须从数组中删除该struct元素。

您只能使用new来创建delete。在您的情况下,struct process readyProcesses[IN_USE]是一个硬编码数组,不是动态创建的,所以您不能删除它。你需要的是一个动态创建的数组——这意味着readyProcesses必须是一个指向数组的指针。

最后:当你新建一个对象时,你使用delete,但当你新建一个对象数组时,你需要使用delete[]

下面是两者的例子:

const int IN_USE=5;
struct process
{
    /*...*/
};
struct ReadyQueue
{
    int size;
    struct process * Active; // Pointer to another struct 
    struct process * readyProcesses; // contains 5 elements of struct 
} ready;
int main()
{
    ready.Active = new struct process; // just one object
    ready.readyProcesses = new struct process[IN_USE]; // a whole array
    // now do something with ready
    delete[] ready.readyProcesses; // delete the array
    delete ready.Active; // delete the single object
    return 0;
}

如果你想删除单个数组元素,而不是删除整个数组,你可以这样做,但这会使索引不指向任何东西。如果你想删除整个索引以及它所指向的对象(这会使数组更小),那么你应该使用std::vector来代替。

const int IN_USE=5;
struct process
{
    /*...*/
};
struct ReadyQueue
{
    int size;
    struct process * Active; // Pointer to another struct 
    struct process * readyProcesses[IN_USE]; // Array of pointers
} ready;
int main()
{
    ready.Active = new struct process; // just one object
    // Allocate one object for each index in the array
    for(int i=0;i<IN_USE;i++)
        ready.readyProcesses[i] = new struct process;
    // now do something with ready
    if(IN_USE>2) // if there at least three items
    {
        delete ready.readyProcesses[2]; // delete the third item
        ready.readyProcesses[2] = nullptr; // mark it as not pointing to anything
    }
    // now do more stuff with ready
    // Now delete each single object in the array
    // item 2 is nullptr so delete won't do anything with that
    for(int i=0;i<IN_USE;i++)
        delete ready.readyProcesses[i];
    delete ready.Active; // delete the single object
    return 0;
}

如果你在程序中间删除了一个项,你需要将该位置设置为nullptr,这样当你在最后删除数组中的每个索引时,你就不会试图删除已经被删除的东西。

如果你真的想动态分配你可以:

const int IN_USE=5;
struct process
{
    /*...*/
};
struct ReadyQueue
{
    int size;
    struct process * Active; // Pointer to another struct 
    struct process ** readyProcesses; // Pointer to array of pointers
} ready;
int main()
{
    ready.Active = new struct process; // just one object
    ready.readyProcesses = new struct process * [IN_USE]; // an array of pointers
    // Allocate one object for each index in the array
    for(int i=0;i<IN_USE;i++)
        ready.readyProcesses[i] = new struct process;
    // now do something with ready
    // Now delete each single object in the array
    for(int i=0;i<IN_USE;i++)
        delete ready.readyProcesses[i];
    delete[] ready.readyProcesses; // delete the (now empty) array
    delete ready.Active; // delete the single object
    return 0;
}