Copyright

What is a Segmentation Fault?

Instructor: David Gloag
Protecting computer systems from errant programs is an important task these days. In this lesson, we'll take a look at segmentation faults, what they are, what causes them, and how they can be avoided.

Exercising Control

Computer programs perform many tasks for users these days. And for the longest time, they were free to do pretty much whatever they wanted within a system. And why not? They had the entire machine at their disposal. But times are changing.

Systems these days are expected to run many programs, several at the same time. Because of this, new mechanisms have been created that keep everything separate.

We're talking, of course, about segmentation, that process that compartmentalizes a program and gives it its own space to operate within. In conjunction, there is an alert that gets generated when there is a problem called a segmentation fault.

What is a Segmentation Fault?

A segmentation fault is an error that indicates when a program tries to access an illegal memory location. An illegal memory location is one that a program doesn't have the rights to access.

Think of it like the alarm systems many retail stores have. You can pick up an item and walk around in the store with it, but the second you leave, an alarm sounds (assuming that you haven't gone to the cashier first).

Like these alarm systems, segmentation faults protect a computer system from problematic program activities - activities that could compromise, or seriously damage, the system.

Causes

Segmentation faults are caused by a variety of things, and cover a number of areas including memory corruption, and indirect memory access. Indirect memory access is access attempted through another memory location, which is common in programming languages like C and C++ (pointers and references). In fact, it is the most common cause of segmentation faults. Variations include:

  • Memory access outside your boundaries - programs are assigned memory when they run, and the program tries to access memory that was not part of that assignment. This is like trying to use your neighbor's car without permission.
  • Using memory that you have not allocated - within the program's assigned memory you must request some if you wish to use it. An issue occurs when you use that memory without the request. This is like showing up at your favorite campsite without a reservation, only to find that someone else has taken the spot.
  • Writing to read-only memory - some memory is marked as read-only. An issue occurs when you try to write something to it. This is like trying to change your Social Insurance Number (Canada) or your Social Security Number (United States).

How To Avoid Segmentation Faults

From a user perspective, there isn't much you can do to avoid creating segmentation faults, beyond not using the program in question. Avoidance techniques focus on the programming.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support