19
votes
Is something wrong with getType(object)?
The function detectCollision() returns NULL if there's no collision happened. You have to check whether your object is equal to NULL before you pass it to getType().
12
votes
Accepted
getType() causing a seg fault when called on object as returned by detectcollision?
Pay close attention to what detectCollision() returns. What happens when there is no collision? (which will be most of the time). Are you handling that? It appears you are not, hence your segfault....
8
votes
Accepted
Segmentation fault when trying to detect collisions in breakout
You may also have to make sure it's not null before you try to pass it into getType(). detectCollision will return null when there are no collisions.
7
votes
Accepted
segmentation fault in recover
Your problem is here:
while (fread(buffer, 512, 1, filein)==1)
{
//if no jpg found and no jpg id then skip
if (i == 0 && buffer[0] == 0 && buffer[1] == 0 && buffer[2] =...
6
votes
Accepted
segmentation fault - pset5 recover.c
Issues with your program:
First, you're reserving 10 chars for the name of the JPG while the maximum length of the name of any JPG is just 7 (e.g., 0xy.jpg) + 1 for the null terminator (i.e., '\0').
...
6
votes
Accepted
Segmentation Fault in Pset4 when detecting is brick is hit
You have to check whether object is equal to NULL before passing it to getType().
detectCollision() might return NULL in case there's no collision happened.
5
votes
Accepted
Segmentation Fault and Returning 1
You have popped with many questions at the same time. Lets start with the simplest one.
What's the relation between return value of a function and command line arguments?
You don't return 1 because ...
5
votes
Accepted
PSET5 speller segmentation fault
Initially you have declared node *table[N] as global pointer.
As you might have already guessed, all global variables/ pointers are set to default values.(In this case, each pointer is set to NULL).
...
4
votes
Segmentation fault (core dumped) in the updateScoreboard function of pset4
Thankfully I found where I have done a mistake!
It's in the initScoreboard . I didn't add return scoreboard to be used in updateScoreboard because in the beginning of main there's GLabel label=...
4
votes
Accepted
Recover Segmentation Fault
Your code has several issues. First, you're hitting the seg fault because you are trying to close a nonexistent output file when you hit the first signature.
Second, you're declaring img twice ( FILE ...
4
votes
Accepted
Segmentation Fault on Inheritance, Lab 5
You don't actually assign to p->parents[1]
// TODO: Recursively create blood type histories for parents
p->parents[0] = create_family(generations - 1);
p->parents[0] = create_family(...
3
votes
Accepted
Segmentation fault testing for detectCollision returning NULL
I was having a similar problem when I moved from:
if (object == paddle)
to:
if (strcmp(getType(object), "GRect") == 0)
After reading your question, I realized I was probably not handling NULL ...
3
votes
Why does accessing an out-of-bounds array element not cause a segmentation fault?
Accessing an array element that's beyond the boundaries of an array has an undefined behavior. This means that it could cause a segmentation fault, but it doesn't always have to.
3
votes
Accepted
Pset3 Segmentation Fault
One of the skills that are indirectly taught in this class, as well as being one of the most important skills you must learn as a programmer is how to debug a program. You will run into seg faults ...
3
votes
Accepted
pset2 initializing segmentation fault
isblank(_), like isupper() and islower(), takes a single character as an argument. You are passing it an entire string and it's choking on it, causing a seg fault. isblank(name[i]) solves the issue ...
3
votes
Accepted
Problem with edX pset 5 - Spellchecker load - segfaults with large dictionary
For this line:
letterIndex = c - '\'';
try to see what happens if c == 'a', the ASCII code for '\'' is 39. Try fixing this and see what happens
3
votes
Accepted
I just finished pset2 caesar. I get a segmentation fault if argc is other than 2
int k = atoi(argv[1]);
if (argc != 2)
{...........
You are assuming that argv [1] exists before checking that really exists, if we have more than two arguments your program will work well, but if you ...
3
votes
Accepted
pset4 recover.c Segmentation fault
The root cause of your seg fault is this:
char * nameoffile = "";
When you create a string in memory, the space allocated is fixed at that time. In this case, you created a string of length 0 plus ...
3
votes
Accepted
Pset2 Vigenère cipher solution Segmentation fault
You did a pretty good job of narrowing the problem down!
if(isupper(argv[1]))
The isupper() function (and all it's cousins) takes a single char as input, but argv[1] is a string, not a single char. ...
3
votes
Accepted
Vigenere Segmentation understanding
This is a very, very common error for new programmers. The problem is the actual call to isalpha(). The isalpha() function takes a single char as input, but argv[1] is a string, not a char. Simply ...
3
votes
Accepted
Pset4 Recover: Program creates segmentation fault
There are a couple of problems in the code. Ordinarily, I'd suggest that you need to identify which line is causing the seg fault, but it looks like you're stalled out on that. In this case, running ...
3
votes
Accepted
pset4 filter: reflect produces "UndefinedBehaviorSanitizer:DEADLYSIGNAL" forever
for (int i = 0; i < width; i++)
Shouldn't it be height?
3
votes
Accepted
Pset4 Recover conceptually struggling
First, you really, really need to learn how to determine which line is generating a seg fault in any program! Seg faults are going to occur over and over and over. This is one of the most critical ...
3
votes
Accepted
Segmentation Fault while running caesar
The immediate problem lies here:
if ( isalnum(k [1]) || argc == 2)
The isalnum() function requires a single char to be passed to it. k[1] is a string. Pass a string to isalnum or any of it's ...
2
votes
Accepted
Recover.c Segmentation Fault
You're trying to read 2 elements of size 512 bytes into a buffer which I assume is of size 512 bytes only. That's definitely one reason why your program segfaults.
Also, fread() returns the number ...
2
votes
Accepted
"Segmentation fault (core dumped)" error in vigenere
This:
if isalpha(key)
isalpha() works on a char, and you've given it a string. That will segfault. You need to check each individual char, so think of using a loop.
To run in gdb:
gdb ./vigenere
....
2
votes
How to Check Whether a String Contains Non-Alphabetical Characters?
The string argv[] refers to string of strings. So when you are trying to do argv[1] then it means that you are looking for a string, but you cannot use isalpha(string), because the argument passed to ...
2
votes
Segmentation fault when trying to detect collisions in breakout
What type have you initialized the variable "collision" to be? If you initialized it as a string, that could be the issue.
Make sure you are initializing it as:
GObject collision = detectCollision(...
2
votes
Accepted
Why do we not get a warning when passing a string where we should pass an int?
It turns out that isalpha is defined as a macro in the ctype.h header file, in my case, according to this answer (by @ouah on Stack Overflow):
Add -Wsystem-headers to get the warning
From gcc ...
2
votes
Accepted
strcat segmentation fault
First, strcat doesn't work because of the fact that the string literal "a" that's stored in s is stored into a read-only-data section in memory. So you cannot modify its contents.
A simple fix to ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
segmentation-fault × 332pset5 × 98
pset4 × 77
recover × 76
speller × 51
pset2 × 44
load × 35
pset3 × 29
trie × 24
cs50x × 23
caesar × 20
pset6 × 18
vigenere × 17
segfault × 15
check × 14
c × 12
breakout × 12
pset4recover × 10
valgrind × 9
server × 8
unload × 8
malloc × 7
check50 × 6
pset2-caesar × 6
dictionary × 5