0

我正在练习如何通过重载运算符 new 来实现最佳拟合算法。

#include<iostream>
#include<malloc.h>
using namespace std;

#define SUCCESS 0
#define FAILURE -1

#define MAX_PARTITION_SIZE 1000

int *avail_space,*needed_space,*used_space,*done_space,*free_space;
int index,j,k,partitions,nop,flag,total_avail;
int *ptr[10];
int no_obj;

class memory{
        private:

        public:
        void set_data();
        void create_mem();
        int create_partitions();
        int alloc_space();
        int showdata();
        int bestfit_search(int* index);
        void* operator new(size_t size);
        void operator delete(void* p);
};

void* memory :: operator new(size_t size){
        void* p;
        int index_no;
        memory obj;
        int i = 0;
        cout << "In Overloaded new" << endl;
        cout << "Size : - " << size << endl;
        for(i = 0; i < no_obj; i++){
                cout << "address :" << ptr[i] << endl;
        }
        cout << "Size : " << sizeof(memory) << endl;
        needed_space[index] = sizeof(memory);
        obj.bestfit_search(&index_no);      // I guess the problem is in this function where I need to call a non-static member function.
        cout << "Index_no - " << index_no << endl;
        return ptr[index_no];
}

void memory :: operator delete(void* p){
        cout << "In Overloaded delete" << endl;
        free(p);
}

void memory :: set_data(){
        total_avail = MAX_PARTITION_SIZE;
        index = partitions = nop = flag = j = k = 0;
}

void memory :: create_mem(){
        memory* obj[10];
        int index;
        cout << "Enter the no of objects : " << endl;
        cin >> no_obj;
        for(index = 0; index < no_obj; index++){
                cout << "Creating obj : - " << index << endl;
                obj[index] = new memory();
        }
}

/* Allocate memory to all the pointer variables based on the no of partitions */
int memory :: alloc_space(){
        avail_space = new int[partitions];
        needed_space = new int[partitions];
        used_space = new int[partitions];
        done_space = new int[partitions];
        free_space = new int[partitions];
        if((NULL == avail_space) || (NULL == needed_space) || (NULL == used_space) || (NULL == done_space) || (NULL == free_space)){
                return FAILURE;
        }
        return SUCCESS;
}
/* Get the no of partitions and the space needed for each of the partition */
int memory :: create_partitions(){
        cout << "Enter the No of partitions to be created : " << endl;
        cin >> partitions;
        cout << endl;

        /* allocate the space for the pointers */
        if(FAILURE == alloc_space()){
                return FAILURE;
        }

        /* Get the size of each partition and update the freespace*/
        cout << "Enter the space for each of the partitions : " << endl;
        for(index = 0; index < partitions; index++){
                cout << "Partition : " << index << "\t";
                cin >> avail_space[index];
                free_space[index] = avail_space[index];
                ptr[index] =(int*)malloc(avail_space[index]);
                if(ptr[index] == NULL){
                        cout << "Memory allocation failed!" << endl;
                        return FAILURE;
                }
                cout << "ptr : [" << index << "] - " << ptr[index] << endl;
        }
        cout << endl;
        return SUCCESS;
}

/* Display the available, used and free space*/
int memory :: showdata(){
        cout << "Available space  "<< "Used space  "<<"Free space  " <<"Done space "<<endl;
        for(index = 0; index < partitions; index++){
                cout << avail_space[index]<<"\t\t"<<used_space[index]<<"\t\t"<<free_space[index]<<"\t\t"<<done_space[index]<<endl;
        }
        return SUCCESS;
}

/* Algorithm to search for the best fit memory */
int memory :: bestfit_search(int* index_no){
        if(NULL == index_no){
                cout << "Invalid parameter!" << endl;
                return FAILURE;
        }
        int space = 0;
        for(index = 0; index < nop; index++){
                space = needed_space[index];
                flag = 0;
                for(k = space; k < MAX_PARTITION_SIZE; k++){    // MAX_PARTITION_SIZE - 1000
                        for(j = 0; j < partitions; j++){
                                if((space == avail_space[j]) && (done_space[j] != 1) && (flag != 1)){
                                        used_space[j] = needed_space[index];
                                        free_space[j] = avail_space[j] - needed_space[index];
                                        done_space[j] = 1;
                                        cout << "Used space" <<used_space[j] << " freespace" << free_space[index] <<"done space " << done_space[index] <<endl;
                                        flag = 1;
                                        *index_no = j;
                                        break;
                                }
                        }
                        if(flag == 1){
                                break;
                        }
                        space++;
                }
        }
        showdata();
        return SUCCESS;
}

/* Main */
int main(){
        memory obj;
        obj.set_data();
        obj.create_partitions();
        obj.create_mem();
        return SUCCESS;
}

程序运行:

Enter the No of partitions to be created : 
5

Enter the space for each of the partitions : 
Partition : 0   2
ptr : [0] - 0x9e62080
Partition : 1   7
ptr : [1] - 0x9e62090
Partition : 2   3
ptr : [2] - 0x9e620a0
Partition : 3   10
ptr : [3] - 0x9e620b0
Partition : 4   25
ptr : [4] - 0x9e620c0

Enter the no of objects : 
2
Creating obj :
In Overloaded new
Size :  1
address :0x9e62080
address :0x9e62090
Size : 1
Available space  Used space  Free space  Done space 
2       0       2       0
7       0       7       0
3       0       3       0
10      0       10      0
25      0       25      0
Index_no - 0

Creating obj :
In Overloaded new
Size : 1
address :0x9e62080
address :0x9e62090
Size : 1
Available space  Used space  Free space  Done space 
2       0       2       0
7       0       7       0
3       0       3       0
10      0       10      0
25      0       25      0
Index_no - 1

我得到了错误的输出,因为可用空间 2 和 3 必须被已用空间占用为 1。

4

0 回答 0