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
33
votes
5 answers

How do I diagnose this PHP segmentation fault?

I am running a command-line PHP job that is resulting in a Segmentation Fault. This job has worked for a long time, but it processes something that is emailed in. Apparently there's something in this email that's break it, but I have no idea what.…
Dave
  • 1,420
  • 3
  • 17
  • 25
32
votes
3 answers

Why does an infinitely recursive function in PHP cause a segfault?

A hypothetical question for you all to chew on... I recently answered another question on SO where a PHP script was segfaulting, and it reminded me of something I have always wondered, so let's see if anyone can shed any light on it. Consider the…
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
32
votes
6 answers

strcpy()/strncpy() crashes on structure member with extra space when optimization is turned on on Unix?

When writing a project, I ran into a strange issue. This is the minimal code I managed to write to recreate the issue. I am intentionally storing an actual string in the place of something else, with enough space allocated. // #include…
iBug
  • 35,554
  • 7
  • 89
  • 134
32
votes
1 answer

How to find which thread caused SEGFAULT in a post-mortem gdb session?

In my application, I handle SIGSEG to produce a backtrace and call abort() to generate a core dump. If I now run a gdb-post-mortem analysis of the core, the thread which caused the SEGFAULT is no longer visible. Is there anything I can do so I see…
Martin C.
  • 12,140
  • 7
  • 40
  • 52
32
votes
9 answers

Executing machine code in memory

I'm trying to figure out how to execute machine code stored in memory. I have the following code: #include #include int main(int argc, char* argv[]) { FILE* f = fopen(argv[1], "rb"); fseek(f, 0, SEEK_END); unsigned…
user47322
32
votes
1 answer

The shortest C program, still causes segfault

For a moment I was very proud of myself to have written my probably first C bug-free program. Here is the entire source code: int main; It compiles perfectly even without the int, but a warning is issued (even without -Wall) and, as a programmer…
emesx
  • 12,555
  • 10
  • 58
  • 91
31
votes
8 answers

Why is this string reversal C code causing a segmentation fault?

I am trying to write code to reverse a string in place (I'm just trying to get better at C programming and pointer manipulation), but I cannot figure out why I am getting a segmentation fault: #include void reverse(char *s); int main()…
james
  • 313
  • 3
  • 4
30
votes
0 answers

Weird Native Crash - pid: 0, tid: 0 signal 11 (SIGSEGV), code 1 (SEGV_MAPERR)

I am getting this weird crash on android *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** pid: 0, tid: 0 >>> com.oimvo.discdj <<< backtrace: #00 pc 000000000001d050 /data/app/com.oimvo.discdj-2/lib/arm64/libavfilter.7.11.101.so…
Diljeet
  • 1,896
  • 20
  • 24
30
votes
6 answers

Why stack overflow on some machines, but segmentation fault on another?

Just out of curiosity, I'm trying to generate a stack overflow. This code generates a Stack Overflow according to the OP, but when I run it on my machine, it generates a segmentation fault: #include using namespace std; int num =…
Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111
30
votes
2 answers

Can one use libSegFault.so to get backtraces for SIGABRT?

The magic incantation LD_PRELOAD=/lib/libSegFault.so someapp runs someapp with libSegFault.so providing backtrace information on a SIGSEGV as described in many different places. Other than using signal(7)-like approaches to cause SIGABRT to invoke…
Rhys Ulerich
  • 1,242
  • 1
  • 12
  • 28
30
votes
5 answers

Why glibc's fclose(NULL) cause segmentation fault instead of returning error?

According to man page fclose(3): RETURN VALUE Upon successful completion 0 is returned. Otherwise, EOF is returned and the global variable errno is set to indicate the error. In either case any further access (including another call to…
Vdragon
  • 411
  • 1
  • 4
  • 8
29
votes
1 answer

Segmentation Fault, large arrays

#include #define N 1024 int main(){ int i, j; int a[N][N]; int b[N][N]; for (i=0;i
Alexey Matveev
  • 519
  • 1
  • 5
  • 13
29
votes
14 answers

PHPUnit Segmentation fault

When a PHPUnit test fails normally on my dev box (Linux Mint), it causes a "Segmentation Fault" on my Continous Integration box (Centos). Both machines are running the same version of PHPUnit. My dev box is running PHP 5.3.2-1ubuntu4.9, and the CI…
Rudolf Vavruch
  • 495
  • 2
  • 6
  • 13
28
votes
4 answers

Execution of printf() and Segmentation Fault

#include int main() { char *name = "Vikram"; printf("%s",name); name[1]='s'; printf("%s",name); return 0; } There is no output printed on terminal and just get segmentation fault. But when I run it in GDB, I get…
Vikram
  • 1,999
  • 3
  • 23
  • 35
27
votes
2 answers

How to install oracle jdk11 in Alpine linux docker image?

My Dockerfile: FROM frolvlad/alpine-glibc:latest ADD jdk-11.0.6_linux-x64_bin.tar.gz /usr/java ENV JAVA_HOME=/usr/java/jdk-11.0.6 ENV PATH=$JAVA_HOME/bin:$PATH When I run the command java -version in the container, I get this segfault: How can I…
DWG24
  • 271
  • 1
  • 3
  • 3