Thread: Access Violation (segmentation fault) issues with an array ?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Access Violation (segmentation fault) issues with an array ?

    Hey I'm writing a programme to create a random matrix and print it to a file. But when I run it just shuts the programmes down. No errors no warnings nothing.
    I'm using Dev C++ and when I debug I get an error "access violation (segmentation violation).
    I've tested the file out put and it works I've also tested the print_matrix function and it works.
    The issue lies in the rand_matrix function, I've done a bit of research and I think the issue is indexing outside an array or miss-indexing

    Any help is help
    Thanks in Advance

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <time.h>
    #include <malloc.h>
    #include <conio.h>
    
    
    typedef struct matrep {
            unsigned rows, columns;
            double*data;
            } MATRIX;
    
    
    /////////////////////////////////////////////////////////
    double random()
    {
        static int seeded = 0;
        double val ;
        if ( !seeded )
            {
             srand( time(NULL) ) ;
             seeded = 1;
            }
        val = ((double)rand())/ (double)RAND_MAX * 100.0 ;
        return val ;
    }
    ////////////////////////////////////////////////////////
    MATRIX*rand_matrix( struct matrep*mat ) 
    {
       for ( int i=0; i < mat->rows; i++ )
           {
            for ( int j=0; j < mat->columns; j++ )
                 {
                 *(mat->data)++ = random() ;
                 }
            }
      return mat ;
    }
    //////////////////////////////////////////////////////////
    void print_matrix(struct matrep*mat ) 
    {
      int i, j ;
    
    
       if ( (mat->data)==0 || (mat->rows)==0 || (mat->columns)==0 )
           {
           printf("Empty matrix\n" );
           return ;       
            }
        printf( "\n\nMatrix of dimensions %d x %d\n\n", mat->rows, mat >columns) ;
    
    
       for ( i=0; i < mat->rows; i++ )
            {
            for ( j=0; j < mat->columns; j++ )
                 {
                  printf("\t%1.2lf", *(mat->data)++ );
                  }
            printf("\n") ;
             }
                 
    }
    ///////////////////////////////////////////////////////////
    void matrix_write(struct matrep*mat,FILE *fp)
    {
        fprintf(fp,"rows = %d, columns = %d \n",mat->rows,mat->columns);
        for ( int i=0; i < mat->rows; i++ )
            {
            for ( int j=0; j < mat->columns; j++ )
                {
                fprintf(fp, "\t%.2lf", *(mat->data)++ );
                }
            fprintf(fp, "\n" ) ;
             }
    }
    
    
    int main()
    {
    
    
        MATRIX mat;
    
        mat.rows=4;
        mat.columns=5;
    
        MATRIX *ptr;
        ptr=&mat;
    
    
        rand_matrix(ptr);
        print_matrix(ptr);
    
    
         FILE *fp;
         char file[80];
    
        printf("enter file name: ");
        gets(file);
       
        if((fp=fopen(file,"w"))==NULL)
           { 
           printf("Cannot open file %s",file);
           getch();
           exit(1);
            }
        matrix_write(&mat,fp);
        fclose(fp);
    
        printf("\nOperations complete. Hit any key to exit");
     
    
        getch();
        return (0);           
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You haven't allocated any memory for structure element "data".
    Plus it's not a good idea to increment and lose value of the pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation(Segmentation Fault) error
    By nirvana619 in forum C Programming
    Replies: 5
    Last Post: 08-27-2010, 08:43 AM
  2. An Access Violation Segmentation Fault. Need Help ASAP
    By darknessfalls in forum C Programming
    Replies: 2
    Last Post: 08-22-2010, 05:56 AM
  3. access violation segmentation fault raised in your program
    By dinamit875 in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2009, 11:44 AM
  4. Getting Access Violation (Segmentation Fault)
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 11:43 AM
  5. access violation (segmentation fault)
    By MarlonDean in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2008, 05:02 AM

Tags for this Thread