What is Segmentation fault?

Asked

Viewed 9,161 times

12

This error often occurs in code with problems. I don’t see it occurring in other languages. Why does it occur? What does it mean?

  • I always wondered about this, I’ve seen this error in PHP when you run a code on the command line that generates an infinite recursion.

  • @Wallacemaxters is probably a PHP error :)

1 answer

14


This occurs by accessing an unauthorized memory point for your application.

When your application is running, the operating system frees access to virtual memory. Contrary to what many people think, you don’t have access to physical memory (RAM) directly, in fact you might not even be in RAM. It is a problem of the operating system to put where you want, it organizes the memory as you want. It will put your code, static data area, stack and then allocate other snippets dynamically as needed, the heap. Physical memory isn’t usually in sequence, but it seems to be. When your application accesses it looks like the entire memory of the computer is hers, so it’s called virtual memory, the addresses are not physical ones. Of course memory needs to be reserved for its use.

So any address that is accessed by the app and was not reserved is considered an invalid address. Thus there is a protection to the other applications. The memory is divided into segments, so the attempt to access a segment that does not belong to you causes a segmentation failure.

In C or C++ it is common to have this type of problem because the use of pointers is very free and direct. It is relatively easy to make a pointer variable have an invalid value. Mainly by forget to initialize pointer, but also:

  • by doing a wrong arithmetic or taking random value (wild Pointer),
  • treat another die as a pointer,
  • use an address that was existing and is no longer (dangling Pointer),
  • stack overflow,
  • access address beyond a sequence (array), which in other languages is out of range, especially in string unfinished,
  • modify static area, especially literal strings,
  • among others.

It is very common to have a Segmentation fault because of a undefined behavior left in the code.

But do you know what is the address that most causes this problem? It is the 0, ie the null. At this point is equal to the exception Null Pointer or Null Reference of other languages. These languages are not so liberal with the use of pointers, they usually manage access to memory, but they often allow an object to be null. More modern languages are avoiding this and only letting the null occur by choice of the programmer where it makes sense.

Today compilers have options to help identify potential pointer problems and don’t even let them do operations that will create problems. There are extra static or dynamic analysis tools that help even more.

Appearing this error already start to see your pointers, the problem is in one of them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.