Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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[^]
Posted

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[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Sep-12 18:06pm    
Right, a 5.
--SA
Espen Harlinn 29-Sep-12 4:36am    
5'ed!
Just use std::reverse[^].

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


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;
}


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
 
Share this answer
 
v2
Comments
fjdiewornncalwe 28-Sep-12 10:55am    
Even easier. +5.
Espen Harlinn 29-Sep-12 4:36am    
Thank you, Marcus :-D
Sergey Alexandrovich Kryukov 28-Sep-12 18:05pm    
Right, a 5.
--SA
Espen Harlinn 29-Sep-12 4:36am    
Thank you, Sergey :-D
JF2015 29-Sep-12 6:47am    
Nice one. 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900