Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can cause segmentation faults in C++? [closed]

I noticed there's not question with a list of common causes of segmentation faults in C++, so I thought I'd add it.

Naturally it's community Wiki, since there's no one correct answer.

I think this might be useful for newer programmers learning C++, feel free to close it if you disagree.

like image 556
fluffels Avatar asked Aug 03 '11 08:08

fluffels


People also ask

What are the causes of segmentation fault?

The following are some typical causes of a segmentation fault: Attempting to access a nonexistent memory address (outside process's address space) Attempting to access memory the program does not have rights to (such as kernel structures in process context) Attempting to write read-only memory (such as code segment)

What is segmentation fault in C ++=?

CC++Server Side ProgrammingProgramming. A segmentation fault occurs when your program attempts to access an area of memory that it is not allowed to access. In other words, when your program tries to access memory that is beyond the limits that the operating system allocated for your program.


3 Answers

Segmentation fault is caused by bad accesses to memory, only if your OS has a MMU (Memory Management Unit). Otherwise, you won't get it but only strange behavior.

The virtual memory (the entire memory accessible to you = 2^(sizeof(pointer_type)*8) (ie: 2^num_bits_in_pointer_type)) is mapped to physical memory in units named pages or segments (paging superseded segmentation but they are still used).

Each page has some protection rights, if you try to read from a page with no-read access you'll get a segfault. If you try to write to a readonly location you'll get a SIGSEGV.

If you have an unitialized pointer and use it it may happen that it will point to another good location so you'll don't get a segfault. If you have a small array reading after it's bound may corrupt other memory areas if it doesn't get past the page boundary.

Also, since there are many pages, not all of them are really mapped. If you touch a non-mapped page you'll get a segfault. Actually, any access to a non mapped page will have to take into account copy on write, pages on swap, lazy loading, memory mapped files and other things. See this article on page fault handling, especially the second diagram there, posted here below too (but read the article for more explanations)

page fault handling
(source: champ at vistech.net)

You are mainly interested in what happens in user space and all paths leading to SIGSEGV. but kernel space is also interesting.

like image 166
Mihai Maruseac Avatar answered Oct 20 '22 08:10

Mihai Maruseac


Accessing an array out of bounds (Possible):

int ia[10];
ia[10] = 4; // Someone forgot that arrays are 0-indexed! Possible Segfault.
like image 36
Seb Holzapfel Avatar answered Oct 20 '22 08:10

Seb Holzapfel


Dereferencing NULL pointers.

#include <cstddef> //For NULL.
int* p1 = NULL; //p1 points to no memory address
*p1 = 3; //Segfault.
like image 6
fluffels Avatar answered Oct 20 '22 06:10

fluffels