Problems Using memset and memcpy

Posted by user306557 on Stack Overflow See other posts from Stack Overflow or by user306557
Published on 2010-04-01T04:16:31Z Indexed on 2010/04/01 4:23 UTC
Read the original article Hit count: 291

Filed under:
|
|

So I am trying to create a Memory Management System. In order to do this I have a set amount of space (allocated by malloc) and then I have a function myMalloc which will essentially return a pointer to the space allocated. Since we will then try and free it, we are trying to set a header of the allocated space to be the size of the allocated space, using memset.

memset(memPtr,sizeBytes,sizeof(int));

We then need to be able to read this so we can see the size of it. We are attempting to do this by using memcpy and getting the first sizeof(int) bytes into a variable. For testing purposes we are just trying to do memset and then immediately get the size back. I've included the entire method below so that you can see all declarations. Any help would be greatly appreciated! Thanks!

void* FirstFit::memMalloc(int sizeBytes){

node* listPtr = freelist;
void* memPtr;



// Cycle through each node in freelist
while(listPtr != NULL)
{

    if(listPtr->size >= sizeBytes) 
    {
        // We found our space
        // This is where the new memory allocation begins
        memPtr = listPtr->head;
        memset(memPtr,sizeBytes,sizeof(int));

        void *size;
        memcpy(size, memPtr, sizeof(memPtr));
        // Now let's shrink freelist
        listPtr->size = listPtr->size - sizeBytes;

        int *temp = (int*)listPtr->head + (sizeBytes*sizeof(int));  
        listPtr->head = (int*) temp;
        return memPtr;
    }

    listPtr = listPtr->next;
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about memory-allocation