memory allocation class

This is a discussion on memory allocation class within the C++ Programming forums, part of the General Programming Boards category; Hi. I made this class, to emulate segment/offset based memory. it isn't allowed to allocate less then full segment. Anyway, ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    Post memory allocation class

    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...

    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;
    }
    this is the code - i compile it with Borland C++ 3.11 in dos enviroment.

    Thanks in advance.
    Last edited by Orca; 10-05-2001 at 08:08 AM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    I'm not sure what you're doing, but then I'm not sure what I'm doing either, but I did find a problem. It was having problems freeing memory in the destructor because of below.

    Code:
    int* cMem::Save(int Seg, int Offset, const int* Data, size_t n)
    {
           if(!IsAllocated[Seg])
           {
               mem[Seg]=(int*)malloc(size); // allocates 32 bytes    
               IsAllocated[Seg]=1;
           }
           
           return (int*)memcpy(mem[Seg]+Offset, Data, n*sizeof(int));   //copies 10 * 4 (n*sizeof(int) = 40 bytes
            and it copies starting  12 past the allocation location
    }
    
    mem[Seg]=(int*)malloc(size); 
    to 
    mem[Seg]=(int*)malloc(size * sizeof(int)); 32 * 4 = 128 
    Don't know why I used that size but its big enough to cover the copy
    also changed the constructor declaration so you can instaniate the object as follows cMem t; This will create a cMem object size = 32, the default which is established in the constructor declaration.

    cMem(int Size);

    to

    cMem(int Size = 32);

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2
    i dont want to use a default segment size, but you are right about the allocation size - how could i miss that...

    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21