19

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

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

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

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] == 0 && buffer[3] == 0) continue; // check if buffer has either key jpg strings else if (buffer[0] == 0xff && buffer[1] == 0xd8 &...


6

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'). Second, within your for loop for (int i = 0; i < BLOCK; i++) { fread(&buff[i], sizeof(BYTE), 1, card); } you're reading 512 bytes, but you're ...


6

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

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 you have 1 command line argument. The return value of any function(including main()) is almost independent of the arguments passed (unless you do something ...


5

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). So head = table[hash_index]; // It's still NULL When you do head->next, you're trying to de-reference NULL which results in seg-fault.


4

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=initScoreboard and that must have a return value! Thank you for your help ^^


4

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 * img... ). Since the second time occurs inside of a set of curly braces that don't contain the first declaration, the compiler creates a local version of img ...


4

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(generations - 1); // Still parents[0], you meant [1] You print parents[1] here print_family(p->parents[1], generation + 1); // seg fault


3

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 correctly, and so I went back and did a check to make sure the object being returned by getCollision was not equal to NULL. Then, things worked better. In your ...


3

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

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 frequently as a programmer, so you really need to learn how to isolate them yourself! There are a number of techniques you can use. You could step through the ...


3

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 nicely. Also, I don't follow the reason for i++; inside the if loop. The for loop is incrementing i, so do you really want to increment it again inside the loop?...


3

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

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 have only one argument are accessing a memory area to which you should not access and argv [1] "does not exist" hence your segmentation fault Si esto responda ...


3

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 the end of string marker. Later, when you try to create the filename with the sprintf() call, the code tries to insert a longer string into this space, ...


3

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. The line of code tries to shove a string down isupper()'s throat, so it chokes and throws a seg fault. You need to process one char at a time through isupper()....


3

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 put, the code is trying to stuff a string down isalpha's throat and it chokes, resulting in a seg fault. You need to check each char in the string. Hint: a for ...


3

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 debug might take too long to step through until you get to the seg fault. Inserting some carefully placed printf statements would help. However, the seg fault ...


3

for (int i = 0; i < width; i++) Shouldn't it be height?


3

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 skills - identifying which line causes a problem!!!! I can't stress this enough!!!! There are two common ways to track down a seg fault. One is to run the ...


3

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 cousins and you get a seg fault. The next issue is in the same line of code. ALWAYS check the value of argc FIRST. If argc is too small, it means that there aren't ...


2

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 of bytes that were successfully read if the number of elements is 1. I don't really know why you're checking whether the return value is not equal to 1 in this ...


2

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 ... (gdb) run abc will run it with key of abc


2

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 isalpha() is char. It should be like isalpha(argv[1][0]), where argv[1][0] refers to the first character in the second string in the list of strings.


2

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(window, ball);


2

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 documentation (emphasize mine) -Wsystem-headers Print warning messages for constructs found in system header files. Warnings from system ...


2

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 that is to use a char array that's big enough to fit the concatenated strings as well as the null terminator. So char dest[6]; strcat(dest, "hello"); should work ...


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