Thread: what cuses the seg fault with an array

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    what cuses the seg fault with an array

    just a theoretical question this time. Lets suppose i have an array with n elements . Obviously the elements can be indexed by 0 to n - 1. However the question is does pointing the index at element n cause a seg fault or trying to access the data stored there cause the seg fault.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can point at the Nth element just fine.

    So this is legal.
    for ( p = array ; p < &array[N] ; p++ )

    Dereferencing such a pointer however is always a bad idea.
    Whether you get a segfault because of it depends on your luck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Whether you get a segfault because of it depends on your luck.
    I.e., if you're lucky you WILL get a seggy.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by cooper1200 View Post
    just a theoretical question this time. Lets suppose i have an array with n elements . Obviously the elements can be indexed by 0 to n - 1. However the question is does pointing the index at element n cause a seg fault or trying to access the data stored there cause the seg fault.
    Seg faults sre usually generated by the OS.

    On most hardware the segfault occurs when you try to access a virtual memory address where the OS doesn't know how to map it to a physical address.

    Most common is for addresses around 0 when a NULL pointer is deferenced, but you also do so by accessing a virtual memory address outside of those assigned to the stack, the heap or the program's code area (and many others type of memory that a process might allocate).

    Virtual memory is given to your process in pages (maybe with a size of 4KB), so if you access outside an array by a little bit you wont usually cause a seg fault. Examples of this could be accessing an array with an index of -1 or that of the number of elements in the array - usually these result in unexpected memory corruption, not a segfault.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault trying to assign a value to an array
    By Vespasian_2 in forum C Programming
    Replies: 12
    Last Post: 04-11-2016, 10:33 AM
  2. srtings and array(where is the my fault)
    By bagiins34 in forum C Programming
    Replies: 3
    Last Post: 03-05-2015, 08:46 PM
  3. 2d array segmentation fault
    By Max2011 in forum C Programming
    Replies: 1
    Last Post: 05-21-2011, 10:30 AM
  4. Segmentation fault while using 2D array
    By Damon Dike in forum C Programming
    Replies: 7
    Last Post: 04-03-2010, 08:23 AM
  5. Segmentation fault with array?
    By whiphub in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2004, 01:51 PM

Tags for this Thread