Created
June 30, 2012 01:48
-
-
Save vedantk/3021726 to your computer and use it in GitHub Desktop.
apply() Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
extern void* apply(void* func, void** args, size_t argc); | |
long bar(int a, int b, long c, long d, int e, long f, long g, int h) { | |
printf( "a = %x\n" | |
"b = %x\n" | |
"c = %lx\n" | |
"d = %lx\n" | |
"e = %x\n" | |
"f = %lx\n" | |
"g = %lx\n" | |
"h = %x\n", | |
a, b, c, d, e, f, g, h); | |
return h; | |
} | |
long foo(long a, long b, long c) { | |
long baz = a ^ b & c; | |
printf("a ^ b & c = %d\n", baz); | |
long args[] = { | |
0xaaddaadd, | |
0xddaaddaa, | |
0xaaaaaaaabbbbbbbb, | |
0x1234123412341234, | |
1, | |
0x4321432143214321, | |
0x9879879879879879, | |
2 | |
}; | |
void* result = apply(bar, (void**) &args, 8); | |
printf("--> %ld\n", (long) result); | |
return (long) result; | |
} | |
int main() { | |
long args[] = {3, 4, 5}; | |
void* result = apply(foo, (void**) &args, 3); | |
printf("-> %ld\n", (long) result); | |
return 0; | |
} | |
/* | |
$ ./test | |
a ^ b & c = 7 | |
a = aaddaadd | |
b = ddaaddaa | |
c = aaaaaaaabbbbbbbb | |
d = 1234123412341234 | |
e = 1 | |
f = 4321432143214321 | |
g = 9879879879879879 | |
h = 2 | |
--> 2 | |
-> 2 | |
*/ |
This implementation doesn't handle types larger than 8 bytes. You might be interested in libapply (https://github.com/davvid/libapply), which is a much more general implementation.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about arguments of different types?