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 6055113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:13:41+00:00 2026-05-23T08:13:41+00:00

Why does the code below work without any crash @ runtime ? And also

  • 0

Why does the code below work without any crash @ runtime ?

And also the size is completely dependent on machine/platform/compiler!!. I can even give upto 200 in a 64-bit machine. how would a segmentation fault in main function get detected in the OS?

int main(int argc, char* argv[])
{
    int arr[3];
    arr[4] = 99;
}

Where does this buffer space come from? Is this the stack allocated to a process ?

  • 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-23T08:13:41+00:00Added an answer on May 23, 2026 at 8:13 am

    Something I wrote sometime ago for education-purposes…

    Consider the following c-program:

    int q[200];
    
    main(void) {
        int i;
        for(i=0;i<2000;i++) {
            q[i]=i;
        }
    }
    

    after compiling it and executing it, a core dump is produced:

    $ gcc -ggdb3 segfault.c
    $ ulimit -c unlimited
    $ ./a.out
    Segmentation fault (core dumped)
    

    now using gdb to perform a post mortem analysis:

    $ gdb -q ./a.out core
    Program terminated with signal 11, Segmentation fault.
    [New process 7221]
    #0  0x080483b4 in main () at s.c:8
    8       q[i]=i;
    (gdb) p i
    $1 = 1008
    (gdb)
    

    huh, the program didn’t segfault when one wrote outside the 200 items allocated, instead it crashed when i=1008, why?

    Enter pages.

    One can determine the page size in several ways on UNIX/Linux, one way is to use the system function sysconf() like this:

    #include <stdio.h>
    #include <unistd.h> // sysconf(3)
    
    int main(void) {
        printf("The page size for this system is %ld bytes.\n",
                sysconf(_SC_PAGESIZE));
    
        return 0;
    }
    

    which gives the output:

    The page size for this system is 4096 bytes.

    or one can use the commandline utility getconf like this:

    $ getconf PAGESIZE
    4096
    

    post mortem

    It turns out that the segfault occurs not at i=200 but at i=1008, lets figure out why. Start gdb to do some post mortem ananlysis:

    $gdb -q ./a.out core
    
    Core was generated by `./a.out'.
    Program terminated with signal 11, Segmentation fault.
    [New process 4605]
    #0  0x080483b4 in main () at seg.c:6
    6           q[i]=i;
    (gdb) p i
    $1 = 1008
    (gdb) p &q
    $2 = (int (*)[200]) 0x804a040
    (gdb) p &q[199]
    $3 = (int *) 0x804a35c
    

    q ended at at address 0x804a35c, or rather, the last byte of q[199] was at that location. The page size is as we saw earlier 4096 bytes and the 32-bit word size of the machine gives that an virtual address breaks down into a 20-bit page number and a 12-bit offset.

    q[] ended in virtual page number:

    0x804a = 32842
    offset:

    0x35c = 860
    so there were still:

    4096 – 864 = 3232
    bytes left on that page of memory on which q[] was allocated. That space can hold:

    3232 / 4 = 808
    integers, and the code treated it as if it contained elements of q at position 200 to 1008.

    We all know that those elements don’t exists and the compiler didn’t complain, neither did the hw since we have write permissions to that page. Only when i=1008 did q[] refer to an address on a different page for which we didn’t have write permission, the virtual memory hw detected this and triggered a segfault.

    An integer is stored in 4 bytes, meaning that this page contains 808 (3236/4) additional fake elements meaning that it is still perfectly legal to access these elements from q[200], q[201] all the way up to element 199+808=1007 (q[1007]) without triggering a seg fault. When accessing q[1008] you enter a new page for which the permission are different.

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

Sidebar

Related Questions

In the code below, why does the open function work but the close function
Why does the code below return true only for a = 1? main(){ int
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
In the below code sample, what does {0:X2} mean? This is from the reflection
I usually use IIS in conjunction with the OutSystems development platform, which does code-generation
This code does not seem to compile, I just need to write something to
The following code does not compile: public class GenericsTest { public static void main(String[]
The following Code does not compile Dim BasicGroups As String() = New String() {Node1,
Why does this code fail to display the category name Apples using the current
Why does this code: class A { public: explicit A(int x) {} }; class

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.