変な動作
クイズ:なんでこんな動作をするのだろう? (これはイタズラコード; インテル32ビットCPU + gcc)
/* trick.c */void bar()
{
printf("Hi, this is bar.\n");
printf("... byebye from bar.\n");
exit(0);
}unsigned _bar = (unsigned)&bar;
void foo(int arg) {
unsigned *p = (unsigned *)(&arg - 1); // IA32 + gcc
*p = _bar;
printf("This is foo.\n");
}main()
{
printf("This is main.\n");
foo(0);
printf("Byebye from main.\n");
}
イタズラの悪のり:
/* countdown.c */void countdown(int *);
void mark(int);main(int argc)
{
int c = 10;
mark(0);
printf(" %2d\n", c);
countdown(&argc);
}unsigned _mark;
void mark(int arg)
{
_mark = *((unsigned *)(&arg - 1));
}void countdown(int *pargc)
{
unsigned *p = (unsigned *)(&pargc - 1); // IA32 + gcc
*p = _mark;int *pc = pargc - 3;
if (*pc <= 0) {
printf("BANG!\n");
exit(0);
}
(*pc)--;
}