本文介绍了如何实施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[^]

推荐答案


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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 23:18