Thread: Segmentation fault

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    Segmentation fault

    I'm probably just being stupid, but I can't figure out for the life of me why I'm still getting a segmentation fault on this one... any suggestions?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int ** MatrixMult( int **A, int **B, int n );
    
    int main( int argc, char *argv[] ) {
    
      int **x, **y, n, i, j;
      FILE *fin;
    
      fin = fopen( argv[1], "r" );
    
      fscanf( fin, "%d", &n );
    
      x = ( int ** ) malloc( sizeof( int ) * n );
      y = ( int ** ) malloc( sizeof( int ) * n );
    
      for( i = 0; i < n; i++ ){
    
        x[i] = ( int * ) malloc( sizeof( int ) * n );
        y[i] = ( int * ) malloc( sizeof( int ) * n );
    
      }
    
        for( i = 0; i < n; i++){
    
          for( j = 0; j < n; j++){
    
    	fscanf( fin, "%d", &n );
    	x[i][j] = n;
    
          }
    
        }
    
        for( i = 0; i < n; i++ ){
    
          for( j = 0; j < n; j++ ){
    
    	fscanf( fin, "%d", &n );
    	y[i][j] = n;
    
          }
    
        }
    
      y = MatrixMult( x, y, n );
    
      fclose ( fin );
    
      return 0 ;
    }
    
    int ** MatrixMult ( int **A, int **B, int n ){
    
      int **z, i, j, k;
    
      z = ( int ** ) malloc( sizeof(int) * n );
    
      for ( i = 0; i < n; i++ ){
    
        z[i] = ( int * ) malloc( sizeof(int) *n );
    
      }
    
      for ( i = 0; i < n; i++ ){
    
        for ( j = 0; j < n; j ++ ){
    
          for ( k = 0; k < n; k ++ ){
    
    	z[i][j] += A[i][k] * B[k][j];
    
          }
    
        }
    
      }
    
      return z;
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    x = ( int ** ) malloc( sizeof( int ) * n );
    y = ( int ** ) malloc( sizeof( int ) * n );
    it should be:
    Code:
    x = ( int ** ) malloc( sizeof( int* ) * n );
    y = ( int ** ) malloc( sizeof( int* ) * n );
    but even better:
    Code:
    x = malloc( sizeof( x[0] ) * n );
    y = malloc( sizeof( y[0] ) * n );
    Likewise, although this is correct:
    Code:
    x[i] = ( int * ) malloc( sizeof( int ) * n );
    y[i] = ( int * ) malloc( sizeof( int ) * n );
    you should write it as:
    Code:
    x[i] = malloc( sizeof( x[i][0] ) * n );
    y[i] = malloc( sizeof( x[i][0] ) * n );
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Awesome! Thanks!

    Unfortunately that didn't fix my segementation fault...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, you applied the correct fix everywhere, and still have a segmentation fault? Time to use a debugger as I do not intend to debug this for you
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    M'kay. Thanks anyway :-)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, and this looks wrong:
    Code:
    fscanf( fin, "%d", &n );
    x[i][j] = n;
    You should not be changing n. You probably should be passing the address of x[i][j] instead.

    A few other things to note:
    • Check that argc > 1
    • Check that the file was opened successfully.
    • Free the memory allocated when you are done.
    • Make your indentation a little more consistent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'll throw you a bone:
    Warning 5 warning C6386: Buffer overrun: accessing 'y[i]', the writable size is 'sizeof(int)*n' bytes, but '12' bytes might be written: Lines: 8, 9, 11, 13, 15, 16, 18, 20, 21, 18, 20, 21, 18, 25, 27, 29, 30, 27, 25, 36, 38, 40, 41 41
    Warning 6 warning C6386: Buffer overrun: accessing 'x[i]', the writable size is 'sizeof(int)*n' bytes, but '12' bytes might be written: Lines: 8, 9, 11, 13, 15, 16, 18, 20, 21, 18, 20, 21, 18, 25, 27, 29, 30 30
    Warning 7 warning C6011: Dereferencing NULL pointer 'y[i]': Lines: 8, 9, 11, 13, 15, 16, 18, 20, 21, 18, 20, 21, 18, 25, 27, 29, 30, 27, 25, 36, 38, 40, 41 41
    Warning 8 warning C6011: Dereferencing NULL pointer 'x[i]': Lines: 8, 9, 11, 13, 15, 16, 18, 20, 21, 18, 20, 21, 18, 25, 27, 29, 30 30
    Warning 9 warning C6011: Dereferencing NULL pointer 'y': Lines: 8, 9, 11, 13, 15, 16, 18, 20, 21 21
    Warning 10 warning C6011: Dereferencing NULL pointer 'x': Lines: 8, 9, 11, 13, 15, 16, 18, 20 20
    Warning 11 warning C6011: Dereferencing NULL pointer 'z[i]': Lines: 56, 58, 60, 62, 60, 66, 68, 70, 72 72
    Warning 12 warning C6011: Dereferencing NULL pointer 'z': Lines: 56, 58, 60, 62 62
    The last number on the line is the line to where the warning was issued.
    You should re-think the design to eliminate those warnings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Annoying Segmentation Fault
    By Zildjian in forum C++ Programming
    Replies: 7
    Last Post: 10-08-2004, 02:07 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM