Jump to content
  • Advertisement
Sign in to follow this  
E-Lo

My Memcopy

This topic is 4524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wanting to write my own memcopy function and am pretty sure I got it right. Any of you out there see anything that may be wrong with this? void* memcpy( void* dest, void* src, size_t size ) { byte* pDest = (byte*)dest; byte* pSrc = (byte*)src; assert( dest != NULL && src != NULL ); while( size-- > 0 ) *pDest++ = *pSrc++; return (pDest); }

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Quote:
Original post by E-Lo
byte* pDest = (byte*)dest;
byte* pSrc = (byte*)src;


What's a byte? (Hint: unsigned char)
If this is C, why are you casting? (Hint: it's not needed)

Share this post


Link to post
Share on other sites
And why re-invent the wheel when better systems exist?

I'd assert before you do the byte * pDest = (byte *)dest;, why waste time when its not going to work?

Share this post


Link to post
Share on other sites
If you're trying to implement the C standard library memcpy() function, you got the return value wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
If you're trying to implement the C standard library memcpy() function, you got the return value wrong.


How is that so? Isn't pDest the pointer to where it was moved, which is what I would want to know?

Share this post


Link to post
Share on other sites
Quote:
Original post by E-Lo
Quote:
Original post by SiCrane
If you're trying to implement the C standard library memcpy() function, you got the return value wrong.


How is that so? Isn't pDest the pointer to where it was moved, which is what I would want to know?


pDest now points to the last byte of your copied range.

You should return dest

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Mike2343
And why re-invent the wheel when better systems exist?

I'd assert before you do the byte * pDest = (byte *)dest;, why waste time when its not going to work?


If it were C, then you couldn't do it in that order. In fact, the assert() isn't required by either C or C++, why waste time on the comparison? It won't even catch the "overlapping objects" bug.

Share this post


Link to post
Share on other sites
In C++ you shouldn't even use memcpy or memmove, but instead std::copy from the <algorithm> header. It will call constructors when it has to, and generally will optimize down to the same executable code as memcpy or memmove (as appropriate) when possible (i.e. if this seems not to be the case for you, you should upgrade your compiler).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!