Re: Implementing my own memcpy
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Sat, 25 Jun 2005 17:05:08 GMT
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
.
- References:
- Implementing my own memcpy
- From: Rajan
- Re: Implementing my own memcpy
- From: Netocrat
- Re: Implementing my own memcpy
- From: Rajan
- Implementing my own memcpy
- Prev by Date: Re: Implementing my own memcpy
- Next by Date: What about a C compiler with structured data management INSIDE ?
- Previous by thread: Re: Implementing my own memcpy
- Next by thread: Re: Implementing my own memcpy
- Index(es):
Relevant Pages
|
|