Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6910787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:51:34+00:00 2026-05-27T08:51:34+00:00

Here is my code, #include<signal.h> #include<stdio.h> int main(int argc,char ** argv) { char *p=NULL;

  • 0

Here is my code,

#include<signal.h>
#include<stdio.h>

int main(int argc,char ** argv)
   {
     char *p=NULL;
     signal(SIGSEGV,SIG_IGN); //Ignoring the Signal
     printf("%d",*p);
     printf("Stack Overflow"); //This has to be printed. Right?
   return 0;
    }

While executing the code, i’m getting segmentation fault. I ignored the signal using SIG_IGN. So I shouldn’t get Segmentation fault. Right? Then, the printf() statement after printing ‘*p’ value must executed too. Right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:51:35+00:00Added an answer on May 27, 2026 at 8:51 am

    Your code is ignoring SIGSEGV instead of catching it. Recall that the instruction that triggered the signal is restarted after handling the signal. In your case, handling the signal didn’t change anything so the next time round the offending instruction is tried, it fails the same way.

    If you intend to catch the signal change this

    signal(SIGSEGV, SIG_IGN);
    

    to this

    signal(SIGSEGV, sighandler);
    

    You should probably also use sigaction() instead of signal(). See relevant man pages.

    In your case the offending instruction is the one which tries to dereference the NULL pointer.

    printf("%d", *p);
    

    What follows is entirely dependent on your platform.

    You can use gdb to establish what particular assembly instruction triggers the signal. If your platform is anything like mine, you’ll find the instruction is

    movl    (%rax), %esi
    

    with rax register holding value 0, i.e. NULL. One (non-portable!) way to fix this in your signal handler is to use the third argument signal your handler gets, i.e. the user context. Here is an example:

    #include <signal.h>
    #include <stdio.h>
    
    #define __USE_GNU
    #include <ucontext.h>
    
    int *p = NULL;
    int n = 100;
    
    void sighandler(int signo, siginfo_t *si, ucontext_t* context)
    {
      printf("Handler executed for signal %d\n", signo);
      context->uc_mcontext.gregs[REG_RAX] = &n;
    }
    
    int main(int argc,char ** argv)
    {
      signal(SIGSEGV, sighandler);
      printf("%d\n", *p); // ... movl (%rax), %esi ...
      return 0;
    }
    

    This program displays:

    Handler executed for signal 11
    100
    

    It first causes the handler to be executed by attempting to dereference a NULL address. Then the handler fixes the issue by setting rax to the address of variable n. Once the handler returns the system retries the offending instruction and this time succeeds. printf() receives 100 as its second argument.

    I strongly recommend against using such non-portable solutions in your programs, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
Here's a simple C file: #include <stdio.h> #include <stdlib.h> int main() { printf(hi there!\n);
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
Here is my code: #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; int
I am going to paste here my code and errors: #include stdio.h #include winsock2.h
Here is my code which checks if the file exists : #include<stdio.h> #include<zlib.h> #include<unistd.h>
Here is my code: #include <gtk/gtk.h> static int counter = 0; static PangoLayout* layout;
Consider the code : #include <stdio.h> class Base { public: virtual void gogo(int a){
I have this code: #include <stdio.h> #include <stdlib.h> #include <signal.h> // Define the function
Here's the code. Not much to it. <?php include("Spreadsheet/Excel/Writer.php"); $xls = new Spreadsheet_Excel_Writer(); $sheet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.