How to predict where segmentation fault occurs if we are increasing a pointer in a infinite loop?
I have thought that we can use the alarm signal handler and it will be invoked after every 1 second and it will print the address pointed to by the pointer so that when the segmentation fault would occur we would get a very nearby address where it occurred.
Could there be more better solution? If there then please tell. Thanks in advance
Code :
#define SECOND 1
int *str;
void ALARMhandler(int sig)
{
signal(SIGALRM, SIG_IGN);
printf("ptr=%u \n",*str);
alarm(SECOND); /* set alarm for next run */
signal(SIGALRM, ALARMhandler); /* reinstall the handler */
}
int main()
{
int *ptr;
str=&ptr;
int a;
ptr=&a;
signal(SIGALRM,ALARMhandler);
alarm(SECOND);
for(;;)
{
ptr++;
}
return 0;
}
No comments:
Post a Comment