Re: how to optimize a for loop



On Tue, 30 Oct 2007 19:15:52 -0700, andreyvul <andrey.vul@xxxxxxxxx>
wrote in comp.lang.c:

On Oct 30, 9:53 pm, Eric Sosman <esos...@xxxxxxxxxxxxxxxxxxxx> wrote:
andreyvul wrote:
I have this loop (all variables are pointers):
for (foo = bar; foo > baz; foo--)
*(foo+1) = *foo;
How do I optimize the pointer swap so that it uses -- and ++ or unary
+- instead of +1 (if possible - I don't want to have more #defines
than code)?
IOCCC winners can really help me with this :P

Assuming the pointers are of the same type:

memmove (baz + 2, baz + 1, (bar - (baz + 1)) * sizeof *baz);

--
Eric Sosman
esos...@xxxxxxxxxxxxxxxxxxxx
sort still fails
actual output: 1 1 3 1 1 1 1 3 1 1
intended output: 1 2 3 4 5 6 7 8 9 10
somehow memmove fails while iterated swap works. wierd.

Then there is an error in your code, which you have failed to
recognize.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.