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. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    So what are some good numbers to type in?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    6
    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. #5
    Registered User
    Join Date
    May 2011
    Posts
    6
    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. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) Avoid using global variables as much as possible

    2) Your printfs assume integer and float data rather than double (%lf).
    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.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    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$
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Von Fuzzball View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #11
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    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
    Last edited by rags_to_riches; 05-03-2011 at 04:47 PM.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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. #13
    Registered User
    Join Date
    May 2011
    Posts
    6
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-17-2011, 11:55 AM
  2. Replies: 3
    Last Post: 06-05-2010, 03:58 AM
  3. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 04:50 PM
  4. Runtime error 'segmentation fault (core dumped)'
    By mikemccready in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:14 AM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21