Thread: Segmentation Fault

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Segmentation Fault

    Any idea why this is giving me a segmentation fault ?

    Code:
    for (j=0;j<=g->n_vertices;j++){
    		printf("\t\t EDGE %d %d \n",aux2->numerovertice,aux2->a->destino->numerovertice);
    		if(aux2->a->prox->destino!=NULL) {
    		printf("\t\t EDGE %d %d \n",aux2->numerovertice,aux2->a->prox->destino->numerovertice);}
    		aux2=aux2->seg;
    		}
    It prints the first three well and then just gives me a segmentation fault

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, you compile it with debug
    gcc -g prog.c

    Then you load the program into a debugger
    gdb a.out

    Then you run the code and wait for the crash.

    Then you do things like
    print aux2
    print aux2->a
    until you find the garbage pointer.
    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.

  3. #3
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Thanks Salem but how do I run the code ?
    Sorry, I've never used gdb before.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by DeanWinchester View Post
    Thanks Salem but how do I run the code ?
    Sorry, I've never used gdb before.
    Type 'run' into the GDB prompt.

    Type 'man gdb' for other commands.

  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
    Really, you don't say.....
    Code:
    $ gdb
    GNU gdb (GDB) 7.1-ubuntu
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>.
    (gdb) help
    List of classes of commands:
    
    aliases -- Aliases of other commands
    breakpoints -- Making program stop at certain points
    data -- Examining data
    files -- Specifying and examining files
    internals -- Maintenance commands
    obscure -- Obscure features
    running -- Running the program
    stack -- Examining the stack
    status -- Status inquiries
    support -- Support facilities
    tracepoints -- Tracing of program execution without stopping the program
    user-defined -- User-defined commands
    
    Type "help" followed by a class name for a list of commands in that class.
    Type "help all" for the list of all commands.
    Type "help" followed by command name for full documentation.
    Type "apropos word" to search for commands related to "word".
    Command name abbreviations are allowed if unambiguous.
    (gdb)
    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
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Hmm, thanks a lot, gonna start using this!

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    My magic 8-ball says it might be this:
    Code:
    for (j=0;j<=g->n_vertices;j++){
    But really, that's just a guess.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By phoneix_hallows in forum C Programming
    Replies: 8
    Last Post: 08-27-2009, 05:56 AM
  2. Segmentation fault?
    By Camambert in forum C++ Programming
    Replies: 2
    Last Post: 08-20-2009, 11:21 AM
  3. Segmentation Fault
    By jat421 in forum C Programming
    Replies: 6
    Last Post: 04-03-2005, 02:26 PM
  4. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM