123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|734|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> Functions returning multiple results?

Tue, 18 Sep 2007, 16:24
HoboBen
Hi, anyone got a good link handy for ANSI C and getting functions returning more than one result?

I've googled a bit but found nothing for C. Maybe there's some other way of doing it?

If that's not a good idea/possible, then riddle me this:

I'm swapping two integers using this C code:


Is there a way to turn that into a function or something else so that I can just swap(a,b) just like that?

Much thanks you

-=-=-
blog | work | code | more code
Tue, 18 Sep 2007, 16:57
mike_g
xor swapping, or its equivalent using + and - avoids using a temp variable, but its slower. If its something you plan on using intensively I'd do something like what agent smith posted.
Tue, 18 Sep 2007, 17:39
HoboBen
Thanks Smith. I almost had it cracked, but didn't quite know how to do it. That's perfect.

Cheers mike - I actually figured it would be faster not to have a third variable, but I suppose adding and subtracting works out slower as there's more that the computer has to do.

-=-=-
blog | work | code | more code
Tue, 18 Sep 2007, 20:41
mike_g
I was skeptical about a Xor swap being faster than a swap using a temp variable so I made a test prog to time them. On my computer the temp swap always came out fastest. Admittedly only by around 15%, which isent really worth worrying about. Also using a static temp variable gave it a slight extra speed boost. Anyway Heres the code:

Oh, and I dident use macros because they cant work with pointers.
Wed, 19 Sep 2007, 06:31
mike_g
Wow macros are quite fast. I never bothered using them in the past, but I might start.

Also I turned the time up, but I get the same results are pretty much the same for me, but yeah I guess it mostly depends on the machine and compiler. I'm using gcc with on a laptop with an athlon processor.
Wed, 19 Sep 2007, 10:08
HoboBen
Thank you both very much - that's some great information

Have a nice day

-=-=-
blog | work | code | more code
Wed, 19 Sep 2007, 11:34
Phoenix
If you really want the best performance, you could add some inline assembly code. This code won't work, the inline asm syntax differs from every compiler. But it shouldn't be very hard to change.