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().
user avatar
  • 17.4k
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....
user avatar
  • 18.4k
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.
user avatar
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] =...
user avatar
  • 7,326
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'). ...
user avatar
  • 17.4k
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.
user avatar
  • 17.4k
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 ...
user avatar
  • 3,316
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). ...
user avatar
  • 307
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=...
user avatar
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 ...
user avatar
  • 63.4k
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(...
user avatar
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 ...
user avatar
  • 166
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.
user avatar
  • 17.4k
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 ...
user avatar
  • 63.4k
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 ...
user avatar
  • 63.4k
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
user avatar
  • 120
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 ...
user avatar
  • 5,172
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 ...
user avatar
  • 63.4k
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. ...
user avatar
  • 63.4k
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 ...
user avatar
  • 63.4k
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 ...
user avatar
  • 63.4k
3 votes
Accepted

pset4 filter: reflect produces "UndefinedBehaviorSanitizer:DEADLYSIGNAL" forever

for (int i = 0; i < width; i++) Shouldn't it be height?
user avatar
  • 20.7k
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 ...
user avatar
  • 63.4k
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 ...
user avatar
  • 63.4k
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 ...
user avatar
  • 17.4k
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 ....
user avatar
  • 18.4k
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 ...
user avatar
  • 3,316
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(...
user avatar
  • 1,317
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 ...
user avatar
  • 17.4k

Only top scored, non community-wiki answers of a minimum length are eligible