segmentation faults pervade

This is a discussion on segmentation faults pervade within the C Programming forums, part of the General Programming Boards category; I've been struggling with seg faults the entire time I've been developing my program, but I've been able to overcome ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    10

    segmentation faults pervade

    I've been struggling with seg faults the entire time I've been developing my program, but I've been able to overcome until recently.

    I won't post the entire program (unless requested) since its ~600 lines long and uses some application-specific API functions, but hopefully some direction will get me straightened out.

    Q: is the following code snippet valid?

    Code:
    void WriteToLogfile(char message[50]);
    ...
    ..
    .
    WriteToLogfile("this is a sample string\n\0");
    I'm asking to make sure that putting the '\0' at the end will end the string appropriately when being passed to the function.

    Currently, my program compiles and runs *almost* successfully. It performs everything that is asked of it. My debug logging shows that it finishes all of its routines successfully, but it still cores (seg faults) at the very end when run. What is strange to me is that after the last successful operation it only returns and exits. Any assistance with understanding this behavior and more importantly its potential causes would be greatly appreciated.

    I haven't programmed C in over 10 yrs. (if you even consider my college "traveling salesman" algorithms to be programs) so please be gentle!

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    WriteToLogfile("this is a sample string\n\0");
    The \0 is unneeded since its a string literal. I'll wager that the problem is that you are trying to modify the parameter inside of WriteToLogfile(). You can not do this because string literals are allowed to be placed in read only memory so when you try to modify it the program blows up. To make sure you aren't modifying the parameter change it to:
    Code:
    void WriteToLogfile(const char message[50]);

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Thanks for the info. I've made the suggested corrections without luck, however, I'm sure that its a step in the right direction.

    So, any experience with a program that does everything correctly but then cores at the very end? Could a rogue pointer be to blame? I would think that my program would bomb out when the pointer is referenced or otherwise used - and not at the very end when there is literally nothing else to do but a couple of 'return 0's.

    I'm struggling, and any suggestions or advice would be most appreciated.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Only time I've had the program seg fault on exit is when I was doing some inline assembly.

    Now if you have a rogue pointer that points to some memory that is needed for when your program exits then it could be getting corrupted.

    Your best bet is to "comment" out sections of code and retest them bit by bit. You could try some memory profiler tools also.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Code:
    gcc -g prog.c -lefence
    gdb a.out
    May be a start. Electric Fence aims to trap memory access faults at the point they occur (and not sometime later which is what a segfault is). Running the code in the debugger should then give you a nice back-trace to the actual line of code which is causing the problem.
    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.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Ok. Now, what to do with the output. Hmm. Could someone tell me if this looks right or if I'm having problems with my libefence.a?

    Code:
    (gdb) bt
    #0  0x1002c72c in do_abort () at print.c:27
    #1  0x1002cc34 in EF_Abortv (
        pattern=0x200004c8 "fr\e\e(%a): addr\ess not from malloc().", 
        args=0x2ff22514 "/+0 !\\") at print.c:137
    #2  0x1002ccb0 in EF_Abort (
        pattern=0x200004c8 "fr\e\e(%a): addr\ess not from malloc().")
        at print.c:146
    #3  0x1002c2f4 in free (address=0x2ff22b30) at efence.c:749
    #4  0x10034c80 in FreeARValueStruct ()
    #5  0x10001710 in cf_ValidateANI () at remedy.c:591
    #6  0x100019d0 in main () at remedy.c:652
    (gdb)
    I also get the following when running my program after compiling with the -lefence option, but I'm not sure if it's just efence reorting an error with my program or if it is failing itself:

    # ./remedy

    Electric Fence 2.2.0 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>

    ElectricFence Aborting: free(2ff22b40): address not from malloc().
    Illegal instruction


    This (efence) utility is pretty slick. I just need some help ramping up on the particulars. -thx

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Well, it looks like you're trying to pass free() a memory address that wasn't returned by malloc(). That's bad and could definitely be the cause of your seg fault.
    If you understand what you're doing, you're not learning anything.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > ElectricFence Aborting: free(2ff22b40): address not from malloc().
    As itsme says, you did one of

    Code:
    char foo[10];
    free( foo );   // oops, this isn't what malloc returned
    
    char *p = malloc( 10 );
    p++;  // change the pointer
    free( p );   // oops, this isn't what malloc returned
    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
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    thanks all. I was able to find where I was freeing memory without an explicit corresponding 'malloc'. Also, I appreciate the referral to Electric Fence - this is a super-handy tool that I'll be using from now on.

    Thanks again!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation faults when initializing large arrays
    By MathFan in forum C++ Programming
    Replies: 5
    Last Post: 07-14-2008, 05:24 AM
  2. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  3. oldiofclose.c and segmentation faults
    By sd_padilla in forum C Programming
    Replies: 1
    Last Post: 12-11-2005, 01:24 PM
  4. Segmentation faults on Linked Lists. (Please help!!)
    By summerrainx in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2005, 06:23 AM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM

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