简体   繁体   English

如何在C ++中释放结构和对象的数组?

[英]How to free an array of structure and objects in C++?

The structure of the program is as such, there is a header file manager.h in which C++ class is defined with data members and member functions. 程序的结构就是这样,有一个头文件管理器.h,其中C ++类是用数据成员和成员函数定义的。 Then in a manager.C file the member functions are implemented. 然后在manager.C文件中实现成员函数。

I've defined 3 structs in the .c file. 我在.c文件中定义了3个结构。

#define HTSIZE 7

struct Node
{
int value;
int page;   
struct Node *next; 
};

struct Node *hashtable[HTSIZE];


struct lruNode{
int value;
struct lruNode *next;
struct lruNode* head;
struct lruNode* tail;

};

struct lruNode* lruhashtable[HTSIZE];
struct lruNode* lruTrackHead;
struct lruNode* lruTrackTail;


struct mruNode{
int value;
struct mruNode *next;    
 };

 struct mruNode* mruTrackHead=NULL;
 struct mruNode* mruhashtable[HTSIZE];

The data members of class are as follows: Class Name:Page 类的数据成员如下:类名:Page

Class Name:Frame 类名称:框架

Class Name:Manager 类名:经理

All the structs are declared in manager.C 所有结构都在manager.C中声明

manager.h has the following data members. manager.h具有以下数据成员。

Page* pagePool;
Frame* framePool;

In manager.C I'm defining them as 在manager.C中,我将它们定义为

pagePool=new Page[n];
framePool=new Frame[n];

In the destructor for manager.C 在manager.C的析构函数中

I'm doing 我正在做

delete[] pagePool;
delete[] framePool;
*hashtable=NULL;
*lruhashtable=NULL;
*mruhashtable=NULL;

The structs here are global and not part of the class as such. 这里的结构是全局的,因此不是类的一部分。 This is part of my homework. 这是我作业的一部分。 The problem is in my test-cases I'm carry forwarding the previous test case values. 问题出在我的测试用例中,我需要转发以前的测试用例值。

I did an edit to set the pointers the way they are. 我进行了编辑以将指针设置为原样。 Now the first value in the array for all array of pointers to struct is null. 现在,所有指向struct的指针数组在数组中的第一个值为null。

Still the array of pointers to objects are not freed up. 仍然没有释放指向对象的指针数组。 Can someone comment on that? 有人可以对此发表评论吗?

Edit 编辑

On iterating through a loop, the pointers are freed. 在遍历循环时,将释放指针。

But still the array of pointers to objects are not? 但是仍然不是指向对象的指针数组吗?

when you call 你打电话时

free(hashtable); 免费(哈希表);

you dealocate memory alocated for an array of 您释放分配给数组的内存

int value;
int page;   
struct Node *

but the memory alocated for structure that is pointed by Node is note dealocated. 但是为Node指向的结构分配的内存已取消分配。

The main idea is to dealocate from the "deepest pointed" structure to top. 主要思想是从“最深的尖头”结构到顶部。 So try to do a bottom up dealocation ,in reverse order that it was alocated. 因此,尝试以自下而上的方式进行自下而上的脱位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM