Click here to Skip to main content
12,451,446 members (58,935 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C C#
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 28-Sep-12 4:07am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov 28-Sep-12 18:06pm
   
Right, a 5.
--SA
Espen Harlinn 29-Sep-12 4:36am
   
5'ed!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
Marcus Kramer 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+
Espen Harlinn 29-Sep-12 6:47am
   
Thank you, JF2015
steven8Gerrard 29-Sep-12 15:24pm
   
Solution has been posted for C++ . I just want to know about memrev function in C
Espen Harlinn 29-Sep-12 17:46pm
   
Follow the link, and look at the C++ implementation, a c implementation would look pretty similar. In my implementation the swap is performed by the three memcpys' before adjusting the pointers.
pasztorpisti 29-Sep-12 19:55pm
   
+5, didn't even know about the existence of std::reverse but I never needed it in practice. For the OP I would mention that in C++ memrev can't be used and *might* cause problems when the array item type is not a POD instance.
Espen Harlinn 30-Sep-12 4:20am
   
Thank you - I added the c implementation because OP came back and asked for it ...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100