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 ...

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:

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):

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?

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.

1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

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$

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

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.

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!