abort() causes a segfault

This is a discussion on abort() causes a segfault within the C Programming forums, part of the General Programming Boards category; When I call abort() in an if statement, it causes a segmentation fault, if I do exit(0), it exits fine. ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    abort() causes a segfault

    When I call abort() in an if statement, it causes a segmentation fault, if I do exit(0), it exits fine. But I need to use abort(), can anyone provide the details of what abort() exactly does? I googled it, but I don't much about what this function actually does to abort the program
    =========================================
    Everytime you segfault, you murder some part of the world

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    If it helps, I get an error saying

    "6 [sig]" along with something else like cored dumped corrupted stack.
    =========================================
    Everytime you segfault, you murder some part of the world

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Do you have a signal handler installed for SIGABRT, or are you possibly using a library of functions which might have installed a signal handler for abort? (Which, by the way, is 6).
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I don't know what exactly a signal handler is. Neither did I install such a thing, so I'm guessing it's to do with the latter (library functions)
    =========================================
    Everytime you segfault, you murder some part of the world

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Can you post your code?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I will post a part of it because it's assignment work and I don't want someone else here to copy it, if they happen to be in my class.

    I ran the gdb earlier and it indicated a problem was at this part:

    Code:
    AR node;
    AR head = NULL;
    AR tail;
    AR next;
    int N = 0; //line number-node number
    
    /*PROTOTYPE DECLARATIONS*/
    AR makeNode(void);
    AR traverseE(AR ar); // function for traversing to the end of the list
    
    int main (int argc, char *argv[]) {
    Of course it didn't indicate which line so I had no idea.

    This is where I call the abort function:

    Code:
      if((pos1 > N) || (pos1 < 0) || (pos2 > N) || (pos2 < 0)) {
        printf("Error position out of range\n");
        abort();
      }
    Essentially N is the number of nodes and if a position entered by user is greater than that or less than 0, just abort.
    =========================================
    Everytime you segfault, you murder some part of the world

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    ??? Can't tell from that.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by Todd Burch View Post
    ??? Can't tell from that.

    Well do you know exactly how the abort function works? If I knew that I could probably figure out what exactly it's doing before it exits.
    =========================================
    Everytime you segfault, you murder some part of the world

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    What platform are you using?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I just read up on abort(). It's doing just what it is supposed to do.

    It is terminating your program, and sending the SIGABRT signal to the caller of your program, and the caller of your program is taking a core dump just like it is supposed to.

    I suspect you'll need to find a new mechanism to exit clean if you want to exit clean.

    You can verify this by writing a small "hello world" program and aborting instead of returning.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  11. #11
    Day Dreamer
    Join Date
    Apr 2007
    Posts
    45
    The abort() function causes abnormal process termination to
    occur, unless the signal SIGABRT is being caught and the
    signal handler does not return. The abnormal termination
    processing includes at least the effect of fclose(3C) on all
    open streams and message catalogue descriptors, and the
    default actions defined for SIGABRT. The SIGABRT signal is
    sent to the calling process as if by means of the raise(3C)
    function with the argument SIGABRT.

    The status made available to wait(3C) or waitpid(3C) by
    abort will be that of a process terminated by the SIGABRT
    signal. abort will override blocking or ignoring the
    SIGABRT signal.
    That's what the description on the man page says.

    And the Usage says:
    Catching the signal is intended to provide the application
    writer with a portable means to abort processing, free from
    possible interference from any implementation-provided
    library functions. If SIGABRT is neither caught nor
    ignored, and the current directory is writable, a core dump
    may be produced.
    I guess that makes it clear to you why your code is dumping core.
    I'd recommend writing a signal handler to work with SIGABRT (6) for a clean exit or use other means exiting your code.
    of clean up and exit.
    I would love to change the world but they dont give me the source code!

  12. #12
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    No it's not clear to me exactly. Though I understand there is some kind of stray signal so the streams haven't been completely closed. I think I'm going to try to fix up my code instead of writing a signal handler, we haven't learn such a thing and I don't think the lecturer intended for such a thing. It was meant to be a simple clean exit.
    =========================================
    Everytime you segfault, you murder some part of the world

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Abort was not made for a simple and clean exit. It's more meant if something fatal happens in the program and it needs to be terminated directly without allowing the flow of the program to continue.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 08:13 AM
  2. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 12:24 PM
  3. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 01:38 PM
  4. abort()
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2008, 04:11 AM
  5. Segfault and Warning help
    By Uncle Rico in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 02:51 PM

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