在c++中,从结构体中删除成员会产生错误
Deleting a member from a struct gives error in c++
在我的代码中,我有一个这样声明的结构体。
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;
}
相关文章:
- 获取 C2039:"交换":不是成员错误,如何解决?
- C++类析构函数删除成员(如果"owner"?
- C++引用已删除函数错误
- 在Visual Studio中删除成员std::vector::d ata()
- C++ 链表删除了错误的项目
- 删除2D_Array错误
- 在C 中获取Invalig使用非静态数据成员错误
- 非静态数据成员错误的使用无效
- 为什么删除成员变量的复制构造函数不会阻止默认的复制构造函数
- 引用 std::atomic <bool>的已删除函数错误
- 运行时检查失败#2 - 变量周围的堆栈'myArray'已损坏.- 似乎无法弄清楚如何删除此错误
- (C++)删除矩阵错误中具有重复项的行
- 队列类中的非空删除成员函数
- 是非静态数据成员错误的CPPReference定义
- 删除阵列错误
- 如何在删除类指针列表时删除分段错误
- 带有自定义删除程序错误的 c++ 唯一指针
- C++二进制搜索树删除分段错误
- SQLite删除数据库错误
- 在c++中,从结构体中删除成员会产生错误