What is Memory Access Violation?

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

All programs access computer memory. When something goes haywire, it's not good for either user or programmer. This lesson will discuss memory access violations and ways of avoiding this dreaded malady.

Memory and Memory Issues

Memory Access - think

Computer programs access system memory for processing. Humans draw upon our own stored memory to perform daily tasks. When a computer program tries to access a piece of memory that doesn't exist or otherwise access memory the wrong way (we'll cover this later), it will crash.

Consider the following scenario.

A memory access violation, also called a segmentation fault (or segfault), occurs when the program tries to access a memory location that doesn't exist, or is otherwise inaccessible. We call this trying to access an illegal memory location. That memory is either non-existent or we aren't aren't allowed to touch it.

In order to understand this concept more, let's look at memory itself. Program memory is made up of segments (thus the keyword segmentation fault), or blocks. Let's look at those segments.

Memory segments

The following table highlights each segment.

Segment Description
Text The text segment includes the programming instructions, e.g., the code of your program
Data Variables and arrays that are defined when the program is compiled/built, are stored in the data segment
Stack This is where temporary variables are stored; those defined in functions and subroutines of the program
Heap Variables that have their memory allocated during run-time are stored here. Languages like C/C++ use a function called malloc to allocate memory; in Fortran it's allocate.

Memory Access Violations

Now that we've covered the basics of memory and how programs use it, we can get back to our problem: when the program tries to think, but nothing happens.

The segmentation fault will happen if one of the variable types listed above tries to access memory outside of the segment where it lives. It can also happen if a segment is set to read-only. We've all had that happen in our daily lives; trying to copy or save files and they are read-only. It is the same concept, only the MEMORY is read-only, and the program can't make the update to the variable.

One of the most common examples of memory errors occurs with arrays. An array is a single variable that can hold multiple buckets. Think of a roster for a baseball team's starting roster. It will have 9 buckets. Once you set up the array in your program, there will be consequences if you try to write or access the 10th bucket. Remember: Programs start counting at ZERO! Our first batter is batter 0, the ninth is batter 8.

The following code snippet shows how easy these errors can occur because we've tried to reference the 9th batter. We have 9 batters, right? Yes, but if we count the first as 0, then 9 is out of range!

Memory access error - array

Pointers, which are variables that hold a memory location instead of a value, can cause memory access violations. Languages like C/C++ allow the use of pointers. A function (e.g., scanf()) could be misused and try to point to a memory location that doesn't exist or is read-only.

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