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:48

>>27
hi lambda, this is offtopic but since I saw you I want to ask, what's the point of ptrdiff_t? why not size_t? it seems the same to me

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.

Name: Anonymous 2014-04-27 11:15

>>33
I though that ptrdiff_t was for subtracting pointers to the same object

Name: Anonymous 2014-04-27 11:36

>>34
You could also use it to compare two objects in a heap, two array indices, etc.
Like I said, not a problem on typical 32/64 bit OSes, but imagine a microcontroller or DSP with a 24 or even 16-bit address space. You could easily have an array or heap on such a platform whose length will not fit into a pointer-sized signed integer.

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