Thread: Strange segmentation fault

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    151

    Strange segmentation fault

    I have this problem.
    When I run my program. The whole program gets executed and then I gets a segmentation fault.
    Explanation: From my main file
    Code:
    int main(void)
    {
    PROGRAM
    printf("Process completed");
    return 0;
    }
    I gets a segmentation fault after Process completed. But the whoole program is run.
    Please advise.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    151
    As I am dealing with storing input text in an array line by line; and processing the contents (in this program). I even tried clearing the array.
    But I still get the same problem.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Post the program. I have had this happen to me, I bet it's something to do with the program. Since you mentioned arrays, you are probably going out of bounds somewhere.
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    151
    That would be difficult; as my program calls functions from other source files.

    You said you faced such issues.
    Maybe if you can clue me on what could be the causes. That will be great.

    I also tried something else.I have tagged the whole program with '/*' tag; and just stored each line of the input text into array and displayed the array.

    I still get the segmentation fault. My array size is 100000.

    Please advise.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Have you ever used a debugger?
    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.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There is probably a buffer overrun in PROGRAM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by Ron View Post
    That would be difficult; as my program calls functions from other source files.

    You said you faced such issues.
    Maybe if you can clue me on what could be the causes. That will be great.

    I also tried something else.I have tagged the whole program with '/*' tag; and just stored each line of the input text into array and displayed the array.

    I still get the segmentation fault. My array size is 100000.

    Please advise.
    Well, I can't remember the specific cases, but I have had it happen to me several times. But I'm pretty sure it's in the main areas of code itself, not the part you posted. You mentioned some arrays and arraysizes of 10,000, are you actually using up all of that? Check your iterations to see if they are going out of bounds.
    =========================================
    Everytime you segfault, you murder some part of the world

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You sometimes get segmentation faults after a program exits when you do strange stuff with memory. (Usually malloc() in my case, but I'm the sure same holds true for ordinary arrays.) You're almost certainly going beyond the end of the array or something like that.

    Tools such as Valgrind (which is Linux-only) can help you detect these sorts of errors. (I'm not familiar with systems that work on windows, but you might try electricfence or dmalloc or something like that . . . .)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    151
    I have not used malloc, just structures, and pointers. But as I said in my earlier email. I even tried, just reading each line from the input text with scanf and no pointers into an array and displaying the array contents. The contents are displayed without problems, but I still get the segmentation fault in the end. My program otherwise uses only pointers and structres.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You most likely have a local array variable in main() which is getting overrun somehow. It's probably not some further-down function which has the bug -- if a downslope function was overrunning an array badly enough to screw up the return from main(), it would have itself crashed while attempting to return. The details, of course, lie in "PROGRAM" which you have conveniently omitted.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    151
    This is the code which just reads the input file using the UNIX redirection operator '>' and displays the output.
    Code:
    	
    int main(void)
    {
    	char code[100000];
    
            while (scanf("%s", code) != EOF)
    	{
    	}
    		printf("\n %s",code);
    	}
    		
    
    	printf("\n \n \n File was successfully verified");
    	return 0;
    }

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Ron View Post
    This is the code which just reads the input file using the UNIX redirection operator '>' and displays the output.
    That code will overrun the buffer if the input is larger than 100000 bytes (with no intervening whitespace). Of course, I can't say if that's the case for your SPECIFIC input, but it's buggy and would certainly be one way the crash could happen.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Since the latest code doesn't even compile, mis-matched braces, are you ever going to get to the point of actually posting the real code which crashes?

    Or are you going to continue to waste everyones time with pointless guessing games based on your random attempts to approximate the problem?

    There are 1000's of ways you could have screwed up, and we'd still be no nearer the answer than we are now.
    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.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    151

    Re:

    I told you I cant display the whole code, because this is a university assignment. If caught, I will be expelled.

    I said I have tagged the whole code except the ones displayed with '/*' and my program does run successfully by displaying the contents of the input file. But I get a segmentation fault after 'File was successfully verified'.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Run the program through valgrind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange segmentation fault
    By Lateralus in forum C Programming
    Replies: 2
    Last Post: 06-10-2005, 09:19 AM
  2. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  3. strange segmentation fault
    By lanzyzhang in forum C Programming
    Replies: 4
    Last Post: 07-19-2004, 08:46 AM
  4. strange segmentation fault error!
    By jayjay in forum Linux Programming
    Replies: 1
    Last Post: 10-20-2003, 03:25 PM