如何实施MemRev [英] How to implement MemRev

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

问题描述

你们使用过memrev吗?倒转记忆.
在google中找不到太多信息.有人可以帮忙吗?

我发现的唯一来源是
http://www.manualpages.de/FreeBSD/FreeBSD-ports- 9.0-RELEASE/man3/memrev.3.html
[ ^ ]

示例10.6 http://guideme.itgo.com/atozofc/ch10.pdf [

Have you guys used memrev . Reversing the memory .
Not much info can be found in google . Can anyone help ?

Only source i found is
http://www.manualpages.de/FreeBSD/FreeBSD-ports-9.0-RELEASE/man3/memrev.3.html
[^]

Example 10.6 http://guideme.itgo.com/atozofc/ch10.pdf[^]

推荐答案

我不能说我知道用途是什么,但这可能只是因为我从来不需要这样做.我也不知道您对此有什么疑问.
您只是想实现memrev吗?如果是这样,请不要使算法复杂化.为此,您实际上只需要创建一个与第一个数组大小相同的第二个数组/列表,然后以相反的顺序复制值即可.

只是看一下,发现了这一点: List(T).反向方法 [ ^ ]
I can''t say I see what the use is, but that might just be because I''ve never needed to do this. I''m not sure what your question about it is either.
Are you simply wanting to implement memrev? If so then don''t over complicate the algorithm. To do this you really just need to create a second array/List of the same size of the first and then copy the values in reverse order.

Just took a look and found this: List(T).Reverse Method[^]


只需使用 std :: reverse [
Just use std::reverse[^].

something like:
char data[] = {1,2,3,4};
std::reverse(data,data+sizeof(data));



您正在考虑的c函数可以这样实现:



The c function you are thinking of can be implemented like this:

void *memrev(void *block, size_t elsize, size_t elnum)
{
 char* start = (char*)block;
 char* end = start + (elnum - 1) * elsize;
 char* tmp = (char*)alloca(elsize);

 while (start < end )
 {
  memcpy(tmp,start,elsize);
  memcpy(start,end,elsize);
  memcpy(end,tmp,elsize);

  start += elsize;
  end -= elsize;
 }
 return block;
}



请注意,我将alloca用作临时缓冲区,只要您在堆栈上有足够的可用空间就可以了-这非常快,并且不需要释放内存,因为当函数返回时,这种情况会自动发生.

最好的问候
Espen Harlinn



Note that I use alloca for the temporary buffer, which is fine as long as you have enough free space on the stack - it''s very fast and you don''t need to free the memory as that happens automagically when the function returns.

Best regards
Espen Harlinn


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

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