Write a program that simulates the “heap” using a linked list. This program should
take one node of size 9096 kb. Assume all request will be made in kb. The user
should request a size of memory from the system, then the system should find
memory that can be allocated based on the user’s request. If memory cannot be
allocated based on the user request, send the appropriate message. Memory should
be returned back to the system after it is freed.

Problem:
My freeAllSpace function causes the program to crash...
It is suppose to delete the headUFS linked list and it is suppose to
delete all but the first node on the headFS link list.
#include <iostream>
using namespace std;

struct node
{
     int data;
     node *next;
};

void freeAllSpace(node *,node *);

int main()
{
     node *headFS = NULL;
     int space = 9096;
     headFS = new node;
     headFS->data=space;
     int requestedMEM=-1;
     int temp = 0;
	 int a;

     node *headUFS = NULL;
     headUFS = new node;

     node *previousUFS = NULL;
     previousUFS = new node;

     while(headFS->data>0 && requestedMEM<headFS->data && requestedMEM!=0)
     {
          cout<<"How much memory would you like?";
          cin>>requestedMEM;


          if(requestedMEM==0)
          cout<<"No memory requested."<<endl;


          else if(headFS->data<0 || requestedMEM>headFS->data)
          {
               cout<<"There is not enough free space left."<<endl;
          }

          else
	      {
               temp=temp+requestedMEM;
               headUFS->data = temp;
               previousUFS->data = temp;
               a = headFS->data - previousUFS->data;
			   headFS->data =a;
               headUFS->next;
          }
     }
          
     cout<<"Total Free Space: "<<headFS->data<<endl;
     cout<<"Total Used Space: "<<headUFS->data<<endl;

	 freeAllSpace(headFS,headUFS);
          
     return 0;
}

void freeAllSpace(node *headFS, node *headUFS)
{
	node *temp2;
	node *temp3;

    cout<<"Freeing all space."<<endl;

	while(headUFS->next!=NULL)
	{
		temp2 = headUFS;
		headUFS = headUFS->next;
		delete temp2;
	}

	while(headFS->data!=NULL)
	{
		temp3 = headFS;
		headFS = headFS->next;
		delete temp3;
	}

	cout<<"Total Free Space: "<<headFS->data<<endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.