#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    C++'s memcpy Functions


    I made an attempt to convert C++'s memcpy function to a Python version of it. Here is my current code:
    Code:
        # C++'s memcpy function
        def memcpy(self, dest, src, bytes):
            if(dest < src):
                return;
            new_dest = src[:bytes];
            dest += new_dest;
            return dest;
    memcpy doesn't return anything (correct?), my code does however, this is just how I felt like making the code at the time. I got the description of the function from MSDN. It tells me:
    Code:
    The memcpy function copies count bytes of src to dest.
    I did my best at making a copy of the function, I know the code is incorrect, somewhat. However, I am not sure how the actual memcpy function changes the data it receives. My code only fixes the code up (not sure if it's done correctly) and returns the new buffer, but I know there is more to this function than that. Any help on what I'm missing and how the real memcpy function adds to the new destination buffer?

    Edit: I just realized that I misread soething on MSDN. memcpy returns the new value of dest. Which is what my function does. Is my function correct? Or is something wrong still?
    Last edited by †Yegg†; May 16th, 2005 at 09:47 PM.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Couple of comments:
    1.
    Code:
            if(dest < src):
                return;
    This appears to be useless. What's the purpose, especially if you pass two list objects to it.

    2.
    Code:
    def memcpy(self, dest, src, bytes):
    What's the purpose of self. This isn't part of a class, is it? Besides, you aren't using self anywhere in the function.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    What module are you using to access memory directly - I assume that src and dest aren't normal lists?

    If you wanted to make it a safe copy you could check the two ranges dont overlap.

    if (dest > src + bytes) or (dest < src - bytes) :


    grim
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Originally Posted by Scorpions4ever
    Couple of comments:
    Code:
    def memcpy(self, dest, src, bytes):
    What's the purpose of self. This isn't part of a class, is it? Besides, you aren't using self anywhere in the function.
    It is part of a class.
    Originally Posted by Grim Archon
    What module are you using to access memory directly - I assume that src and dest aren't normal lists?
    I didn't know I needed a module. Yes dest and src are both normal lists. As I've already said, I'm not quite sure how to put this function together, I know the code is incorrect, but I wasn't sure what else was needed and how the code should work.
    Also, it should be:
    Code:
    if(len(dest) < len(src):
        return;
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    i think he was asking what you are using to access the memory that directly, like what module or class? I would like to know as well?
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Originally Posted by CyBerHigh
    i think he was asking what you are using to access the memory that directly, like what module or class? I would like to know as well?
    I realized exactly what he meant about an hour later, after re-reading MSDN. Only I didn't come back here and post that. What module should I use? (I'll look around on Python Docs)
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    IMO This is all something better put into a C module so that the nasty stuff is hidden away. MSDN might be useful after all

    grim
  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Well my new question since I didn't realize this earlier, is what Python module would be good to directly access memory?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    you know, i do not think there is a certain module to actually do that, it may be a good project to start. It would probably have to be writin in c/c++. I would personly think c++ since it is the easyest for memory mangment. I don't see why it would be very hard, there isn't exactly to meny ways to manage memory in c++. The only thing is you gota make it kinda platform independant.
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Couldn't I also use Pyrex for this? Also, there is a DLL file that contains the memcpy function. However, if I wanted this to work on a Linux platform, it may not work this way. I'll see what I can do.

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo