实现我自己的memcpy [英] Implementing my own memcpy

查看:88
本文介绍了实现我自己的memcpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试模拟这样的memcpy

void * mem_cpy(void * dest,void * src,int bytes)

{

dest = malloc(字节);

}

现在,如果我想将字节从src复制到dest,如何我复制这些

字节。

我被困在这里。

解决方案

< blockquote> On Sat,2005年6月25日07:28:30 -0700,Rajan写道:


我正在尝试模拟像这样的memcpy * mem_cpy( void * dest,void *
src,int bytes){
dest = malloc(bytes);
}现在,如果我想将字节从src复制到dest,我是否复制了这些
字节。
我被困在这里。




为什么你不能自己使用memcpy?我会假设你有理由说这不是这是家庭作业。


你想要实现这个目标吗?与memcpy完全相同的行为?如果是这样,

然后不分配内存 - memcpy要求内存已经分配了
。第二个参数需要用const限定。另外

第三个参数的类型是size_t而不是int - 这要求你包含stddef.h

。即使它被声明为int,也应该是

unsigned。


这是一个实现:


# include< stddef.h>


void * mem_cpy(void * dest,const void * src,size_t bytes)

{

unsigned char * srcmax = dest + bytes;


while(src< srcmax)

*(unsigned char *)dest ++ = *(unsigned char *)src ++;

返回dest;

}


" Rajan" < RS ******* @ yahoo.com>写道:

我试图模拟这样的memcpy
void * mem_cpy(void * dest,void * src,int bytes)
{
dest = malloc(bytes);
}现在,如果我想将字节从src复制到dest,我该如何复制这些
字节。




这没有意义。如果你要将字节从

src复制到dest,那么用其他东西替换dest(比如malloc()的

返回值)就没有用了,因为它意味着

你不能再引用目标缓冲区

更长。


有你试过阅读C教程吗?

-

我在我的DeathStation 9000上运行它并且恶魔飞出了我的鼻子。 --Kaz


您好Netocrat,

使用您的代码片段,我发现您使用的是unsigned char *,但是

我可能要复制一个结构。

我们当然不能做* dest ++ = * src ++;如果它们是无效的指针。

那我们该如何处理呢?


Hi,
I am trying to simulate a memcpy like this
void* mem_cpy(void* dest, void* src, int bytes)
{
dest = malloc(bytes);
}
Now if I want to copy the bytes from src to dest, how do I copy these
bytes.
I am stuck here.

解决方案

On Sat, 25 Jun 2005 07:28:30 -0700, Rajan wrote:

Hi,
I am trying to simulate a memcpy like this void* mem_cpy(void* dest, void*
src, int bytes) {
dest = malloc(bytes);
}
Now if I want to copy the bytes from src to dest, how do I copy these
bytes.
I am stuck here.



Why can''t you use memcpy itself? I''ll assume you have a reason that
isn''t "it''s a homework assignment".

Are you trying to achieve the exact same behaviour as memcpy? If so,
then don''t allocate memory - memcpy requires that the memory is already
allocated. The second parameter needs to be qualified with const. Also
the type of the third parameter is size_t not int - this requires that
you include stddef.h. Even if it were declared as int it should be
unsigned.

Here is an implementation:

#include <stddef.h>

void *mem_cpy(void *dest, const void *src, size_t bytes)
{
unsigned char *srcmax = dest + bytes;

while (src < srcmax)
*(unsigned char *)dest++ = *(unsigned char *)src++;
return dest;
}


"Rajan" <rs*******@yahoo.com> writes:

I am trying to simulate a memcpy like this
void* mem_cpy(void* dest, void* src, int bytes)
{
dest = malloc(bytes);
}
Now if I want to copy the bytes from src to dest, how do I copy these
bytes.



This does not make sense. If you are going to copy bytes from
src to dest, then replacing dest by something else (such as the
return value of malloc()) is not going to help, because it means
that you won''t be able to refer to the destination buffer any
longer.

Have you tried reading a C tutorial?
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz


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?


这篇关于实现我自己的memcpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆