Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Greenspun's Tenth Rule

Name: Anonymous 2014-04-25 11:23


#include <stdio.h>
#include <stdlib.h>

void *apply(void *callee, int argc, void *argv) {
void **as = (void**)argv;
void *(*f)(void *a,...) = (void *(*)(void *a, ...))callee;

switch(argc) {
case 0: return ((void* (*)())f)();
case 1: return f(as[0]);
case 2: return f(as[0], as[1]);
case 3: return f(as[0], as[1], as[2]);
case 4: return f(as[0], as[1], as[2], as[3]);
default:
printf("apply: number of arguments limit reached\n");
abort();
}
}

int main(int argc, char **argv) {
char *as[] = {"hello %s\n" , "world"};
apply(printf, 2, as);
return 0;
}

Name: Anonymous 2014-04-27 10:56

>>32
ptrdiff_t has to be a signed integer.
really_large_unsigned_integer - really_low_unsigned integer = overflow when stored in a signed integer of the same width
In practice they're probably the same size on most platforms, since the only way you could get an overflow is by taking the difference of a pointer in kernel space (really high) and user space (really low), and that is not something that you'd ever need to do, but I see where da standed was going with it.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List