# Help with 'Segmentation fault (core dumped)' error?

This is a discussion on Help with 'Segmentation fault (core dumped)' error? within the C Programming forums, part of the General Programming Boards category; Hey everyone. I'm new to C programming. I'm working on a little code of my own right now and it ...

1. ## Help with 'Segmentation fault (core dumped)' error?

Hey everyone. I'm new to C programming. I'm working on a little code of my own right now and it compiles fine, but after the user inserts some data and presses enter, this error pops up. Here's the code:

Code:
```#include <stdio.h>
#include <math.h>

double G, T, Mtot, R, v, B, A, C, Pi, distance;

int main (void)
{
G = 6.67e-11;
Pi = 3.14159;
printf("Enter the combined mass of the systems (kg):");
scanf("%lf", &Mtot);

printf("Enter distance between galaxies (kpc):");
scanf("%lf", &R);
printf("Enter the relative velocity of the approaching galaxy (km/s):");
scanf("%lf", &v);
while (R>=0)
{
C = ((pow(v,2))/2.0) - (G*Mtot)/R;

T = (2.0*Pi*v*(pow(R,2)))/(C*G*Mtot);
printf("\nwhen T = %d years,",T);
printf(" then the other galaxy is %f kpc away",R);
R=R-0.05;
}
system("pause");
return(0);
}```
Once you run it, the segmentation error comes up after the combined mass is entered (Mtot). Any help on fixing this problem would be appreciated.

Oh, and I've been using Cygwin, if that's of any consequence. It's the program we're given for the class I'm in.

2. So what are some good numbers to type in?

3. The easiest way to find a seg fault is with a debugger. You can use gdb with Cygwin, I believe. If you are compiling from the command line, add -g when you compile:

Code:
`gcc -g mycode.c -o myprog`
Now run it in the debugger with "gdb ./myprog". Here's some code guaranteed to seg fault:
Code:
```int main(void) {
int i;
char *p;
for (i=0; ; i++) p[i] = 'x';
return 0;
}```
So for example:
Code:
```[root~/C] gcc -g test.c -o test
[root~/C] gdb ./test
GNU gdb (GDB)
[...blah blah...]
(gdb) run
Starting program: /root/C/test

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400488 in main () at test.c:4
4		for (i=0; ; i++) p[i] = 'x';
(gdb) print i
\$1 = 4880
(gdb) quit```
So we get the line that led to the fault, and I also learned that I in fact went out of bounds by almost 5kb before the OS killed it. Sometimes the offending line will actually be from a library function you've used improperly, in that case use "bt" for backtrace. There are some links to gdb tutorials in my sig, below. Gdb is not the only debugger in the world either.

If your system leaves "core files" when it dumps, you can actually debug a process that's already dead (like doing an autopsy):

Code:
`gdb ./myprog -c coredump.whatever`

4. Oh, right. Here are the numbers I use in the problem for this code:

Mtot = the total mass of the two galactic systems = 3762e39
R = distance between the two galaxies = 0.45
v = velocity of the approaching galaxy = 150

But I suppose you could enter any numbers you'd like, as long as they're positive. If the program can return SOME kind of value instead of the error, then I can continue working with it from there.

5. Ah, thank you, MK27! I remember my professor briefly mentioning something about using gdb to debug programs, but he never really explained it. So after running my code through it, it tells me this (at the end, anyway):

Code:
```Program received signal SIGSEV, Segmentation fault.
0x61124e0 in __svfscanf_r () from /usr/bin/cygwin1.dll```
Does this mean I'm doing something illegal with the variable R in my program?

6. Code:
```while (R>=0)
{
C = ((pow(v,2))/2.0) - (G*Mtot)/R;```
And what happens when R eventually reaches 0?
You can't divide stuff by 0 ... the computer freaks out about it.
You need to define a non-0 exit for that loop such as... While (R > 1.0e-21) ...depending on the precision you need.

7. 1) Avoid using global variables as much as possible

2) Your printfs assume integer and float data rather than double (%lf).

8. Seems kinda OK here, so long as the printf format gets fixed. This gives all the weird answers for T in the results.
This is gcc on ubuntu by the way.

Code:
```\$ gcc -W -Wall -ansi -pedantic -O2 bar.c
bar.c: In function ‘main’:
bar.c:22: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:11: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
bar.c:14: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
bar.c:16: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
\$ ./a.out
Enter the combined mass of the systems (kg):3762e39
Enter distance between galaxies (kpc):0.45
Enter the relative velocity of the approaching galaxy (km/s):150

when T = 1874901181 years, then the other galaxy is 0.450000 kpc away
when T = -294512945 years, then the other galaxy is 0.400000 kpc away
when T = 1404923464 years, then the other galaxy is 0.350000 kpc away
when T = 154157888 years, then the other galaxy is 0.300000 kpc away
when T = -656709046 years, then the other galaxy is 0.250000 kpc away
when T = -294512939 years, then the other galaxy is 0.200000 kpc away
when T = 154157894 years, then the other galaxy is 0.150000 kpc away
when T = -294512929 years, then the other galaxy is 0.100000 kpc away
when T = -294512914 years, then the other galaxy is 0.050000 kpc away
when T = -1043970616 years, then the other galaxy is 0.000000 kpc away\$```

9. Originally Posted by Von Fuzzball
Code:
```Program received signal SIGSEV, Segmentation fault.
0x61124e0 in __svfscanf_r () from /usr/bin/cygwin1.dll```
Does this mean I'm doing something illegal with the variable R in my program?
This is where you want to use bt or backtrace. You'll get a (possibly long) list of cascading function calls, the first one is labelled #0, the next #1, etc. The last one (at least) will certainly be from your code, something like:

Code:
```#7  0x0000000000413d09 in main (argc=1, argv=0x7fffffffdcd8)
at application.cpp:9```
"application.cpp" is my own source file.

10. Ok... I ran this up in Pelles C and here are the errors I got...
Code:
```Building main.obj.
E:\c_Code\Experiments\testconsole\main.c(13): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'float *' but found 'double'.
E:\c_Code\Experiments\testconsole\main.c(16): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'float *' but found 'double *'.
E:\c_Code\Experiments\testconsole\main.c(19): error #2048: Undeclared identifier 'v'.
E:\c_Code\Experiments\testconsole\main.c(19): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'float *' but found 'int *'.
E:\c_Code\Experiments\testconsole\main.c(27): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'double'.
E:\c_Code\Experiments\testconsole\main.c(31): warning #2027: Missing prototype for 'system'.
*** Error code: 1 ***
Done.```
You should be seeing similar errors from your compiler... if you aren't, turn up the warning levels...

11. Are you entering
3762e39
as the value for Mtot? If so, I bet that's the problem.

EDIT: Hmmm...guess not? Don't do a lot of work with reading doubles, so I can claim ignorance of a sort :P

12. No, you must use %lf in the scanf for doubles.
You must supply address of Mtot, as in &Mtot. Hey let's not wreck what was working.
But your printf needs correction. T is double so use %f.
I don't see any reason for seg fault other than possible divide-by-zero.

13. I realized in the text version printed, there was no ampersand in front of Mtot, but that was a previous problem long since fixed in my actual script. But I did make some rather silly mistakes. So after looking over it and modifying it again, it now works. (: I just have to tweak it so the numbers come out correctly. Thanks to all of you again for all of your help! I really appreciate it!