What Causes Segmentation Fault in C++?

Instructor: David Gloag

David has over 40 years of industry experience in software development and information technology and a bachelor of computer science

Working with computers and memory is quite common these days. In this lesson, we'll take a look at segmentation faults, what they are, and what causes them in C++.

Restricting Access

For many years, computer programs had free reign within a system. There were no restrictions on the memory or resources that they could access. In some instances, like embedded systems, this is still the case. Unfortunately, as multi-user systems came into play and single user systems developed the capability to run more than one program at a time, the need for some sort of control emerged. The idea of segmenting (dividing) memory up into pieces was the result. Now, you may be thinking, how does this help? And you'd be right, unless you take into account the control portion: the portion that alerts you when something is amiss . . . the segmentation fault.

What is a Segmentation Fault?

A segmentation fault is an error. Specifically, it is an error that occurs when a program tries to access a memory location that it shouldn't. Think of it like the alarm that sounds when you leave a store with an item you haven't taken through the checkout. As long as you don't leave the store, everything is fine, and you're free to move around as you will. But if you pass the store boundary without going through the checkout with the item, an alarm sounds. Segmentation faults prevent programs from causing havoc within a computer system.

What is C++?

C++ is a programming language created by Bjarne Stroustrup in the late 1970s. It is object oriented by design, meaning that it supports the idea of a system being composed of a set of objects that interact with each other. Its syntax is rather terse yet strangely powerful, providing many low-level features that developers find attractive. Originally developed as an extension to the C language with the extensions converted to C syntax as a pre-processor step, it has become a versatile, full-fledged language. These days, C++ is used on a wide variety of operating systems and in a multitude of applications.

What Causes a Segmentation Fault in C++?

There are a number of things that can cause a segmentation fault in C++. They range from corrupted memory to pointers. However, the most prolific cause of segmentation faults by far is the pointer. If you recall, a pointer is a variable that contains the address of a piece of memory. It allows you to access memory indirectly through another memory location. Common pointer related segmentation faults include:

To unlock this lesson you must be a 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

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
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? 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