Questions tagged [segmentation-fault]

Segmentation faults occur when accessing memory which does not belong to your process. Use this tag along with a tag indicating the language and a tag indicating the operating system. Segmentation faults are typically the result of a dereference operation with pointer variables (most often containing an invalid address) or a buffer overflow. The root cause for an invalid pointer value may be far from the location generating the segmentation fault.

Segmentation faults occur when accessing memory which does not belong to your process. They are common and typically the result of:

  • using a pointer to something that has been deallocated;
  • using an uninitialized hence bogus pointer;
  • using a pointer;
  • overflowing a buffer; or
  • attempting to write to read-only memory

The error does not arise when manipulating the pointer variable itself (copying or assigning the pointer variable), but when accessing the memory the variable points to (i.e. dereferencing the pointer variable). To generate the segmentation fault, will deliver 11 to the process which has made illegal memory access. The default action of having segmentation fault is , generating a coredump file with basic process information.

Since the point where the segmentation fault is triggered may be far from the location where the environment and actions that generate the conditions for the segmentation fault, finding the root cause can be difficult, especially in a complex, multi-threaded application.

Segmentation fault is descriptive phrase from Unix and Linux families of operating systems labeling a general class of behavior in which the operating system detects a memory access by a process outside of the process' assigned memory resulting in the operating system terminating the process.

This behavior requires hardware support for protected memory which may not be available in some microprocessors.

Additional information can be found on...

If the program crashed due to

  1. unauthorized memory access
  2. using out-of-bound memory location
  3. using of uninitialized memory

and it has received SIGSEGV and/or a coredump file is getting generated, mark your questions using this tag.

13352 questions
12
votes
4 answers

Catching segfaults in C

I have a program that segfaults from pointer arithmetic sometimes. I know this happens, but I can't easily check ahead of time to see whether it segfaults or not - either I can "pre-scan" input data to see if it will cause a segfault (which can be…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
12
votes
3 answers

Difference in behaviour (GCC and Visual C++)

Consider the following code. #include #include #include struct XYZ { int X,Y,Z; }; std::vector A; int rec(int idx) { int i = A.size(); A.push_back(XYZ()); if (idx >= 5) return i; A[i].X =…
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
12
votes
3 answers

Why does my program occasionally segfault when out of memory rather than throwing std::bad_alloc?

I have a program that implements several heuristic search algorithms and several domains, designed to experimentally evaluate the various algorithms. The program is written in C++, built using the GNU toolchain, and run on a 64-bit Ubuntu system. …
Brad Larsen
  • 995
  • 1
  • 12
  • 20
12
votes
5 answers

fclose() causes Segmentation Fault

I've been trying simple file handling in C and I wanted to make sure that the file can be accessed tried using this #include main() { CheckFile(); } int CheckFile() { int checkfile=0; FILE *fp1; fp1 =…
user3437503
  • 159
  • 1
  • 1
  • 4
12
votes
1 answer

Clang segfaults when outputting endl

I am trying out Clang (version 3.4, via the Windows pre-built binaries) to see if it could be a suitable replacement for GCC (version 4.8.1, using MinGW); however, I am unable to get a simple program to work. #include int main() { …
Kyle
  • 374
  • 1
  • 11
12
votes
1 answer

std::nth_element causes segfault; am I missing something, or a bug in STL?

I encountered a strange segfault in a big project; finally I managed to locate the code and dump the data. Here is a simplified program: #include #include #include #include using namespace std; const float…
jiakai
  • 501
  • 2
  • 14
12
votes
2 answers

PHP segmentation fault caused by php while loop

I discovered a way to make php segfault, and I'm a bit curious about what's happening. Maybe someone can explain this for me? joern@xps:..com/trunk5/tools/nestedset> cat > while.php ^C 0…
Joernsn
  • 2,319
  • 4
  • 25
  • 33
12
votes
2 answers

Node.js source code build giving segmentation fault on ARM

tl;dr: I tried to install node.js on my ARMv7-based Cubox running Ubuntu 12.10 (quantal). When compiling node.js from source (see "Second attempt" below), node produces a segmentation fault. What can I do here? First attempt First of all, I tried to…
cyroxx
  • 3,809
  • 3
  • 23
  • 35
12
votes
2 answers

print the segmentation fault reason

Let's say I have a code that causes segmentation fault. char * ptr = NULL; *ptr = "hello"; /* this will cause a segmentation fault */ How do I print on runtime, the address in memory that the segmentation fault occurred at, and the reason for the…
Lior
  • 5,841
  • 9
  • 32
  • 46
12
votes
1 answer

cudaMemcpy segmentation fault

I've been haunted by this error for quite a while so I decided to post it here. This segmentation fault happened when a cudaMemcpy is called: CurrentGrid->cdata[i] = new float[size]; cudaMemcpy(CurrentGrid->cdata[i], Grid_dev->cdata[i],…
Stone
  • 345
  • 1
  • 6
  • 14
12
votes
2 answers

segmentation fault using scanf

noob question here: I'm trying to write a simple menu interface, but I keep getting a segmentation fault error and I can't figure out why. #include #include int flush(); int add(char *name, char *password, char *type); int…
agarrow
  • 457
  • 2
  • 7
  • 17
12
votes
5 answers

Segmentation Fault - C

Why does the following code return with a segmentation fault? When I comment out line 7, the seg fault disappears. int main(void){ char *s; int ln; puts("Enter String"); // scanf("%s", s); gets(s); ln = strlen(s);…
bomp
  • 309
  • 1
  • 4
  • 12
12
votes
4 answers

SIGSEGV when entering a function

What can cause a segmentation fault when just entering a function? The function entered looks like: 21: void eesu3(Matrix & iQ) 22: { where Matrix is a struct. When running with GDB the backtrace produces: (gdb) backtrace #0 eesu3 (iQ=...) at…
ritter
  • 7,447
  • 7
  • 51
  • 84
11
votes
3 answers

signal 11 (SIGSEGV), code 1 (SEGV_MAPERR)

I am creating a 2D game on Android using OpenGL. Currently I am testing and debugging the game on several devices. The problem I am facing, is the terrible "signal 11" error. When I am playing on my Samsung Galaxy Nexus, everything runs smooth, and…
Edwin Morren
  • 149
  • 1
  • 1
  • 4
11
votes
3 answers

python ImageTk.PhotoImage - segfault

I am trying to run the following command on a Mac 10.6.8: Python 2.7.2 |EPD 7.1-2 (64-bit)| (default, Jul 27 2011, 14:50:45) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin import Image import ImageTk from Tkinter import Tk window = Tk() i =…
jtlz2
  • 7,700
  • 9
  • 64
  • 114