Hi.
I made this class, to emulate segment/offset based memory.
it isn't allowed to allocate less then full segment.
Anyway, it seems to be working fine, but somethimes it crashes, i don't know why.......
Please look at my code, and give any comments you can...
this is the code - i compile it with Borland C++ 3.11 in dos enviroment.Code:#include <stdio.h> #include <iostream.h> #include <mem.h> #include <alloc.h> #define MAX_SEGMENTS 10 class cMem { private: int *mem[MAX_SEGMENTS]; int IsAllocated[MAX_SEGMENTS]; int size; // segment size public: cMem(int Size); ~cMem(); int* Save(int Seg, int Offset, const int* Data, size_t n); int* Load(int Seg, int Offset); }; cMem::cMem(int Size) { int i; size=Size; for(i=0;i<MAX_SEGMENTS;i++) IsAllocated[i]=0; } cMem::~cMem() { int i; for(i=0;i<MAX_SEGMENTS;i++) if(IsAllocated[i]) free(mem[i]); } int* cMem::Load(int Seg, int Offset) { return mem[Seg]+Offset; } int* cMem::Save(int Seg, int Offset, const int* Data, size_t n) { if(!IsAllocated[Seg]) { mem[Seg]=(int*)malloc(size); IsAllocated[Seg]=1; } return (int*)memcpy(mem[Seg]+Offset, Data, n*sizeof(int)); } //This is just a main to check the class. int main(int argc, char* argv[]) { class cMem Mem(32); int x[10]={9,8,7,6,5,4,3,2,1,0}; const int *ptr; int i; cout << "\n----------\n"; Mem.Save(1, 12, x, 10); ptr=Mem.Load(1, 12); for(i=0;i<10;i++) cout << ptr[i] << " "; return 0; }
Thanks in advance.



LinkBack URL
About LinkBacks


