Re: Implementing my own memcpy



Rajan wrote:
>
> Hi Netocrat ,
> With your code fragment, I find that you are using unsigned char* , but
> I may have to copy a structure.
> We certainly cannot do *dest++ = *src++ ; if they are void pointers.
> So how do we deal with this?

Always include adequate quotation from previous articles, so that
your message stands by itself. There is no reason to assume any
previous material is available to the reader.

One of my sigs reads as follows - heed it if you must use the foul
google interface. Better, get yourself a real newsreader.

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

The point about memcpy is that it copies memory areas, measured in
bytes, with no regard for the types involved. I gather you wish
the same thing, but with automatic creation of the destination. In
this case you need a different signature, such as:

void *dupmem(void *src, size_t sz);

The void * type can point at arbitrary things, and a size_t can
specify a size on any machine. But to use void* you have to
convert to other types, thus:

void *dupmem(void *src, size_t sz)
{
unsigned char *sp = src;
unsigned char *dst;

if (dst = malloc(sz)) /* memory is available */
while (sz--) *dst++ = *sp++; /* copy away */
return dst; /* will be NULL for failure */
} /* dupmem, untested */

Note how src is typed into sp, without any casts. Similarly the
reverse typing for the return value of dupmem. The usage will be,
for p some type of pointer:

if (p = dupmem(whatever, howbig)) {
/* success, carry on */
}
else {
/* abject failure, panic */
}

--
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare


.



Relevant Pages

  • Re: Implementing my own memcpy
    ... But to use void* you have to ... > return dst - sz, unless all your callers will adjust down ... void *dupmem(void *src, size_t sz) ... unsigned char *dst, *p; ...
    (comp.lang.c)
  • [UNIX] SunPCi II VNC Weak Authentication Scheme Vulnerability
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... Virtual Network Computing (VNC) client and server. ... extern char *vncDecryptPasswd(char *fname, unsigned char *key); ... extern void vncEncryptBytes ...
    (Securiteam)
  • [PATCH update5] drivers: add LCD support
    ... * GNU General Public License for more details. ... if not, write to the Free Software ... +void cfag12864b_unset(unsigned char x, unsigned char y) ...
    (Linux-Kernel)
  • Re: "Sorting" assignment
    ... void bacarisseswap(void *vleft, ... void *vright, ... void *heathfieldswap(void *vleft, ... unsigned char *left = vleft; ...
    (comp.programming)
  • TCSBRK(1) on pl2303 USB/serial returns prematurely
    ... unsigned char erase_ee, erase_pf; ... void my_tcgetattr ... void my_tcsetattr(int port_handle, const struct termios *t) ... int my_open ...
    (Linux-Kernel)