从结构中删除成员会在c ++中出错

时间:2016-10-17 02:20:59

标签: c++ struct

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

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;

在我的代码中间,我正在做 delete& ready.readyProcesses [2];

我在运行时出现以下错误

“没有分配被释放的指针”

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

1 个答案:

答案 0 :(得分:0)

您只能delete使用new创建的内容。在您的情况下,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;
}