All Questions

204
votes
16answers
50431 views

Why do I get a segmentation fault when writing to a string initialized with "char *s" but not "char s[]"?

The following code receives seg fault on line 2: char *str = "string"; str[0] = 'z'; printf("%s", str); While this works perfectly well: char str[] = "string"; str[0] = 'z'; printf("%s...
24
votes
2answers
2762 views

Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer

This question is meant to be used as reference for all frequently asked questions of the nature: Why do I get a mysterious crash or "segmentation fault" when I copy/scan data to the address where an ...
390
votes
11answers
396177 views

What is a segmentation fault?

What is a segmentation fault? Is it different in C and C++? How are segmentation faults and dangling pointers related?
78
votes
5answers
70057 views

Segmentation fault on large array sizes

The following code gives me a segmentation fault when run on a 2Gb machine, but works on a 4GB machine. int main() { int c[1000000]; cout << "done\n"; return 0; } The size of the arr...
37
votes
1answers
3154 views

Definitive List of Common Reasons for Segmentation Faults

NOTE: We have a lot of segfault questions, with largely the same answers, so I'm trying to collapse them into a canonical question like we have for undefined reference. Although we have a ...
29
votes
8answers
5292 views

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: #inclu...
199
votes
15answers
211837 views

What is a bus error?

What does the "bus error" message mean, and how does it differ from a segfault?
5
votes
4answers
486 views

Why don't I get a segmentation fault when I write beyond the end of an array?

why is this not giving error when I compile? #include <iostream> using namespace std; int main() { int *a = new int[2]; // int a[2]; // even this is not giving error a[0] = 0; ...
7
votes
1answers
5655 views

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(C...
124
votes
51answers
81491 views

Command failed due to signal: Segmentation fault: 11

I'm getting the error ... Command failed due to signal: Segmentation fault: 11 ... when trying to compile my Swift app. I'm using Xcode 6.1, trying to build for an iPhone 5 on iOS 8.1. My Code ...

Previous Next