0

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. Then in a manager.C file the member functions are implemented.

I've defined 3 structs in the .c file.

#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

Class Name:Frame

Class Name:Manager

All the structs are declared in manager.C

manager.h has the following data members.

Page* pagePool;
Frame* framePool;

In manager.C I'm defining them as

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

In the destructor for 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.

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?

12
  • 3
    How about using std::vector, which will do the dirty job for you. Commented Oct 15, 2012 at 9:03
  • 1
    What is Page ans Frame? Why are you freeing what was not allocated? Commented Oct 15, 2012 at 9:03
  • Why use free for hashtables ? Are you using malloc for them ? Commented Oct 15, 2012 at 9:04
  • Not using malloc for hashtables. Page and Frame are two classes. Commented Oct 15, 2012 at 9:05
  • Unrelated, but I wouldn't recommend using .C for C++ files. Use .cpp. It's more obvious, much more common, will be recognised by more tools, and works on Windows. Commented Oct 15, 2012 at 9:14

1 Answer 1

1

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.

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.

Sign up to request clarification or add additional context in comments.

4 Comments

your architecture is not clear in the question : if node is a part of a list, you can delete a node from the list and link node-1 to node+1 or delete the entire list.
an exemple will be the following struct Node { int value; int page; struct Node *next; }; struct Node *hashtable[HTSIZE]; alocation hashtable = new Node [HTSIZE] for (int i = 0 ; i < HTSIZE ; ++i) { hashtable->Node = new Node(); } dealocation for (int i = 0 ; i < HTSIZE ; ++i) { delete hashtable->Node; } delete [hashtable] ;
@AdrianHerea : you have to begin dealocation with the end,... for (int i = HTSIZE - 1 ; i >= 0 ; --i) { delete hashtable->Node; } delete hashtable;, else you will lose the reference to the third node and the following ones.
in real situation you need to do it in a recursive mode or an itaration method with the same effect. But in my exemple, which is only an exemple, node aren't linked. I just wanted to explain what I want to say with bottom up dealocation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.