Like Tree12Likes

Unsolved Segmentation Fault

This is a discussion on Unsolved Segmentation Fault within the C Programming forums, part of the General Programming Boards category; File opens fine. I've written to it before. In fact, I've run the program before and had no trouble. I ...

  1. #16
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    File opens fine. I've written to it before. In fact, I've run the program before and had no trouble. I made a few changes to it though and thats when it started bugging out. Unfortunately I do not have an earlier version of the file. Anyone have a suggestion for the best lite linux C debugger?

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    I don't know about "lite" debuggers, but GDB is an amazing tool. Not too hard to do a basic run. Compile your code with the -g flag, then load the executable into gdb and run it. When it crashes, get a backtrace.
    Code:
    $ gcc -g -Wall -o foo foo.c
    $ gdb foo
    (gdb) r
    Program received SIGSEGV (or something like that)
    (gdb) bt
    A bunch of useful information about the state of the program and function stack when it crashed.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by uconnhuskies View Post
    File opens fine. I've written to it before. In fact, I've run the program before and had no trouble. I made a few changes to it though and thats when it started bugging out. Unfortunately I do not have an earlier version of the file. Anyone have a suggestion for the best lite linux C debugger?
    Hopefully you now appreciate the importance of backups...

    And "file opens fine" is NOT a good excuse for writing code with no error checking...
    Last edited by CommonTater; 06-22-2011 at 07:26 PM.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,420
    Quote Originally Posted by CommonTater
    Hopefully you now appreciate the importance of backups...
    Use a version control system!
    Matticus, Salem, _Mike and 1 others like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by anduril462 View Post
    I don't know about "lite" debuggers, but GDB is an amazing tool. Not too hard to do a basic run. Compile your code with the -g flag, then load the executable into gdb and run it. When it crashes, get a backtrace.
    Code:
    $ gcc -g -Wall -o foo foo.c
    $ gdb foo
    (gdb) r
    Program received SIGSEGV (or something like that)
    (gdb) bt
    A bunch of useful information about the state of the program and function stack when it crashed.
    To add to this; If you prefer a graphical frontend there's DDD - Data Display Debugger - GNU Project - Free Software Foundation (FSF)
    It has it's quirks and it's not as easy to use as using gdb directly in my opinion, but if you need to watch a lot of variables while stepping through the code it gives a much better overview than gdb*

    *) Or it could just be me not knowing how to use gdb effectively

  6. #21
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Anduril, thanks for the suggestion. I've got to look at gdb documentation to learn to use it a little better, but it seems like this will be great help.

    CommonTater, I'm making sure to use backups now. Ctr-Z did wonders though, I was able redo to my stable version. Rebuilding will hopefully avoid the fault. Also, I do need to start adding more error checking into my code. Thanks for the suggestions.

    Laserlight, I'm going to take a look at version control systems. This seems like it will be extremely helpful!

    _Mike, if I don't like gdb I will take a look at DDD. I have to figure out what environment and software support I like for C. I've got to build a good base for getting out of C beginnerville.

  7. #22
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    So, I went back to my last working version of the program, then i went through and changed some values of arrays, but I incremented them all by the same amount so it should work the same way. Of course, a segmentation fault occurs at the beginning of the program.

    Here is my main:

    Code:
    int main(){
    	//printf("main start\n");
    	struct hostfreq hostCon[1000001];
    	//printf("host con built\n");
    	int i = 0;
    	while(i<=1000000){
    		hostCon[i].freq=0;
    		i++;
    	}
    	addHosts(hostCon);
    	i = 0;
    	//printf("before copy\n");
    	sort(hostCon);
    	/*for(i; i<=1999; i++){
    		printf("Host name %s : %i \n i is %i\n", hostCon[i].hostName, hostCon[i].freq, i);
    	}*/
    	//printf("program complete\n");
    	return 0;
    
    }
    The gdb output looks like this:

    Code:
    Starting program: ***/host 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x08048a4d in main () at hostanalysis.c:145
    145			hostCon[i].freq=0;
    the structure code looks like this:

    Code:
    struct hostfreq{
    	int freq;
    	char hostName[255];
    };
    Any suggestions? Thanks

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by uconnhuskies View Post
    Code:
    struct hostfreq hostCon[1000001];
    ...
    struct hostfreq{
    	int freq;
    	char hostName[255];
    };
    Any suggestions? Thanks
    Your compiler is not a fan of making 247mb arrays.


    Quzah.
    Salem likes this.
    Hope is the first step on the road to disappointment.

  9. #24
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Your compiler is not a fan of making 247mb arrays.
    Quzah.
    Especially not on the program's stack, which is usually about a megabyte.

    Our friend needs to learn how to A) minimize data structures and B) use malloc() and free().
    Salem likes this.

  10. #25
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    Or you could just do this, just to test

    static struct hostfreq hostCon[1000001];


    But you should look at dynamic memory solutions longer term.
    1/4GB of memory is an awful lot to allocate "just in case".
    CommonTater likes this.
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By ankitsinghal_89 in forum C Programming
    Replies: 2
    Last Post: 06-28-2010, 01:45 AM
  2. Segmentation Fault
    By lombardom in forum C Programming
    Replies: 8
    Last Post: 06-06-2010, 03:17 PM
  3. Interview question unsolved for long time...Plz help
    By kush.asthana in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2008, 11:48 PM
  4. Segmentation Fault Again
    By mrbump2004 in forum C Programming
    Replies: 6
    Last Post: 12-05-2004, 01:31 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

Tags for this Thread


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