Thread: Unexplained Segfault

  1. #1
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94

    Unhappy Unexplained Segfault

    Hey all,

    I can't for the life of me figure out why this is segfaulting... sorry to bother you all with such a lame question. Here's the source:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    //#include <math.h>
    
    #define NUM_IN     2
    #define NUM_HIDDEN 3
    #define NUM_OUT    1
    
    //double v[3][3];         //the widest line across has 3 neurons, and 3 layers
    double w[3][3][3];      //for each connection [line][neuron][connection]
    int num[3]={NUM_IN,NUM_HIDDEN,NUM_OUT};
    
    void init_weights() {
      int i,j,k;
      double t;
      
      for (i=1;i<3;i++) {               //layer
        for (j=0;j<num[i];j++) {        //neurons is layer
          for (k=0;k<num[i-1];k++) {    //connections per neuron
            t=((double)(rand()))/((double)(RAND_MAX));
            w[i][j][k]=(t-0.5);         //vals -0.5 <= w <= 0.5
            printf("Setting for layer %d, neuron %d, input %d: %f\n",
                   i,j,k,(t-0.5));
          }
        }
      }
    }
    
    int main( int argc, char *argv[] ) {
      srand(getpid()*time());
    
      init_weights();
    
      return 0;
    }
    And when I run it:

    Code:
    [jack@jacklaptop1 neural-net]$ uname -a;id
    Linux jacklaptop1.crepinc.com 2.6.15-1.2054_FC5smp #1 SMP Tue Mar 14 16:05:46 EST 2006 i686 i686 i386 GNU/Linux
    uid=500(jack) gid=500(jack) groups=500(jack) context=user_u:system_r:unconfined_t
    [jack@jacklaptop1 neural-net]$ gcc -o neural1 neural1.c -lm
    [jack@jacklaptop1 neural-net]$ ./neural1
    Setting for layer 1, neuron 0, input 0: -0.480149
    Setting for layer 1, neuron 0, input 1: -0.267299
    Setting for layer 1, neuron 1, input 0: -0.104947
    Setting for layer 1, neuron 1, input 1: -0.483809
    Setting for layer 1, neuron 2, input 0: 0.079405
    Setting for layer 1, neuron 2, input 1: 0.422783
    Setting for layer 2, neuron 0, input 0: -0.258891
    Setting for layer 2, neuron 0, input 1: 0.237996
    Setting for layer 2, neuron 0, input 2: 0.243387
    Segmentation fault
    [jack@jacklaptop1 neural-net]$
    Thanks for your help,


    -Jack Carrozzo
    jack {-@-} crepinc.com

  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
    > srand(getpid()*time());
    time() has a parameter, but the compiler doens't know that because you didn't include the right headers.
    So the unpassed pointer is just any old thing which is lying around on the stack, which time() duly trashes (probably the return address from main back to the OS).
    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
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    ahhhh you guys are great. I removed the time() call... I knew it was something stupid.

    Thanks again,
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Just as an afterthought, if you had compiled with the -Wall flag, you would have caught that you did not include <time.h>, which would have been quite helpful for you.

    Code:
    gcc -Wall -o neural1 neural1.c -lm

  5. #5
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    is -Wall like "use strict;" in perl then?
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That would probably be more like -ansi. -Wall just turns all the warnings on.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Typically, to make the compiler really picky, I use
    Code:
    gcc -W -Wall -ansi -pedantic -O2 other-arguments
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault for who knows what reason
    By phlook in forum C Programming
    Replies: 4
    Last Post: 03-14-2009, 11:31 PM
  2. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  3. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  4. Unexplained "unhandled exception"
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-19-2007, 11:19 AM
  5. Other programming questions: segfault in free
    By Neeharika in forum C Programming
    Replies: 2
    Last Post: 02-21-2006, 06:35 AM