the code for using setjmp is as follows:
-sharpinclude<assert.h>
-sharpinclude<setjmp.h>
-sharpinclude<stdio.h>
static int ctr;
static jmp_buf b0;
static void jmpto(int n)
{
longjmp(b0,n);
}
static char *stackptr(void)
{
char ch;
return (&ch);
}
static int tryit(void)
{
jmp_buf b1;
char *sp=stackptr();
ctr=0;
switch(setjmp(b0))
{
case 0:
assert(sp==stackptr());
assert(ctr==0);
PPctr;
jmpto(1);
break;
case 1:
assert(sp==stackptr());
assert(ctr==1);
PPctr;
jmpto(2);
break;
case 2:
assert(sp==stackptr());
assert(ctr==2);
PPctr;
switch(setjmp(b1))
{
case 0:
assert(sp==stackptr());
assert(ctr==3);
PPctr;
longjmp(b1,-7);
break;
case -7:
assert(sp==stackptr());
assert(ctr==4);
PPctr;
jmpto(3);
case 5:
return 13;
default:
return 0;
}
case 3:
longjmp(b1,5);
break;
}
return -1;
}
int main()
{
assert(tryit()==13);
printf("sizof(jmp_buf)=%lu\n",sizeof(jmp_buf));
puts("success testing <setjmp.h>");
return 0;
}
There are a lot of assert in the code (sp==stackptr ());
here I don"t understand. How do you make sure that the return value of stackptr is always the value of sp when you call the stackptr function repeatedly in the assert statement?