int memCopy(void* , void*, int size)

This is a discussion on int memCopy(void* , void*, int size) within the C Programming forums, part of the General Programming Boards category; Hi, I am having some fun these days trying to implement c functions using pointers and c prototypes. So far ...

  1. #1
    Unregistered
    Guest

    int memCopy(void* , void*, int size)

    Hi,

    I am having some fun these days trying to implement c functions using pointers and c prototypes. So far i got strlength and stringCopy done. However i am moving along to memcopy and have to admit that i lack some knowlegde or pointers here. The following is what i started out with. I have been going back and forth a while now. Can someone explain how the memcpy function is implemented in c. Or do they use bit manipulation etc.
    I know the below is wrong, but i backed up to the start of me mucking around with it.

    Code:
    int memCopy(void *destination, void *source, int size){
    
       char *dest = (char*)&destination;
       char *sour = (char*)&source;
       int c;
    
       for(c=0;c<size;c++)
          *dest++ = *sour++;
    
        return c;
    
    
    
    }
    Cheers,
    G'n'R

  2. #2
    Sayeh
    Guest
    You're working with raw memory, so use 'unsigned' instead. For speed, don't work with a char unless you have to-- use a long instead. Why copy 1 byte at a time, when you could copy 4? or more (depending on the native word-size of your processor)?

    Never modify your caller's pointers. Always work with a copy. Check for valid pointers, and do some range-checking if necessary. All these things make your routine fast, reliable, robust, and friendly.

    "BADPTR" (below) is a user-defined code-- you could enum it to match whatever this real memcpy() function would return-- I just couldn't remember it at this time.

    This example, just uses, byte-sized copying (but normally I would use unsigned longs)--

    ---

    int memCopy(void *dPtr,void *sPtr)
    {
    unsigned byte *d;
    unsigned byte *s;
    unsigned long blockSize;

    if(!dPtr || !sPtr) /* basic validity check */
    return(BADPTR); /* bad pointer, bail */

    d = dPtr; /* copy original pointers */
    s = sPtr;

    blockSize = labs((long)dPtr - (long)sPtr); /* get size being copied */

    while(blockSize--) /* do the copy */
    d++ = s++;

    return(NOERR); /* success! */
    }


    This is not tested, but it's either right or close.

  3. #3
    Unregistered
    Guest
    cool, thanks for that info Sayah. I will get into your example and write some code soon. I will get back to you on how it goes.


    Cheers
    G'n'R

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Just one minor change:
    Code:
    int memCopy(void *destination, void *source, int size)
    {
    
       char *dest = (char*)destination;
       char *sour = (char*)source;
       int c;
    
       for(c=0;c<size;c++)
          *dest++ = *sour++;
    
        return c;
    }

  5. #5
    Unregistered
    Guest
    Sayah,

    I have been trying out you code and modified it some and more questions have popped into my head. Bear with me. I want to have a clear understanding of what is going on. Here goes.

    >Never modify your caller's pointers. Always work with a copy.
    Question 1
    Arent we here modifying the callers pointers anyway, since the changes made to d and s affect dPtr and sPtr which are the orignal pointers

    Code:
    d = dPtr; /* copy original pointers */ 
    s = sPtr;
    i could only get this to work
    Code:
    while(blockSize--)
       d++ = s++;
    like this. Am i moving the address of the pointer one bit up here or actually dereferencing the pointer an asigning data or doing both?
    Code:
    while(blockSize--)
      *d++ = *s++;


    Question 3
    > You're working with raw memory, so use 'unsigned' instead.
    Why are unsigned variables better.

    Question 4
    >Use unsigned long (in the cast) instead
    Is this because a long is the maximum c variable?

    However, the function does work, except for i really need to insert a null terminator at the end if a pass char arrays. Is it safe to do so on any type passed to this function.

    Thanks again
    G'n'R

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  5. Replies: 2
    Last Post: 03-24-2006, 07:36 PM

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