Thread: fclose function bugging program, or it is just my computer?

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    1

    fclose function bugging program, or it is just my computer?

    Hi, i use live version of ubuntu, and gcc 4.6.3

    Twice happend to me, that fclose bug my program
    First i just move on, commeting fclose, and leave to program automatically close opened files at the end of executation

    First it happens, that fclose mess my memory, so when i use printf on same array, my output is not same... ( fclose is in between two printfs )

    Second it happens, that fclose cause segmentation fault

    Here is second code, and there is two issues
    First, this fclose cause segmentation fault
    Second, when i want to print same array it won't yield same result:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define inputChannel "ulaz4.txt"
    #define outputChannel "izlaz4.txt"
    
    
    void main() {
    
        FILE *in = fopen(inputChannel, "r");
    
        int n;
        fscanf(in, "%d", &n);
    
        int **matrix = (int **)  malloc(sizeof(int) * n );
            
            
    
        int i,j;
        for ( i = 0; i < n; i++ ){
            
            matrix[i] = (int *) malloc(sizeof(int) * n);
    
            for ( j = 0; j < n; j++ ){
                fscanf(in, "%d", matrix+(i*n + j) );
                printf("%d,", *(matrix+(i*n+j)) );}
    
        }
    
        //fclose(in); // here is segmentation fault
        printf("\n");
    
        for ( i = 0; i < n; i++ ){
            
    
            for ( j = 0; j < n; j++ )
                printf("%d,", *(matrix+(i*n+j)) );  // Here is not same result as above
        }
    
        
        
    }
    Is that just becase i use live version of ubuntu, or i'm doing something wrong?

    Here is my output:
    1,2,3,4,
    1,158605712,3,4,

    And here is my input:
    2
    1 2
    3 4

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Are you sure the file is even opening? Perhaps you should check?

    Are you sure one of your malloc() calls is not failing? Perhaps you should check?

    Why are you casting the return value from malloc(), that's not a good practice in C?

    What is the value of n?

    Why the void main(), main should be defined to return an int.

    Your printf and fscanf() look wrong, you probably should be getting some warnings (if you're using a properly configured compiler with adequate warning levels). The variable types don't seem to match the format specifiers which invokes undefined behavior.

    Here are the warnings my compiler generates (after fixing the problem with main()).
    ||=== Build: Debug in c_homework (compiler: gcc 6.1.0) ===|
    main.c||In function ‘main’:|
    main.c|24|warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int **’ [-Wformat=]|
    main.c|25|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]|
    main.c|36|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]|
    ||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 1 second(s)) ===|
    Where are you freeing all that memory you're allocating?

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You are allocating a two-dimensional array but are indexing it as if it was a one-dimensional array simulating a two-dimensional array. You need to make up your mind.

    Here's an example using a 1-d array to simulate a 2-d array.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define INPUT "ulaz4.txt"
    
    int main() {
        FILE *in = fopen(INPUT, "r");
    
        int n = 0;
        fscanf(in, "%d", &n);
    
        int *matrix = malloc(n * n * sizeof *matrix);
    
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                fscanf(in, "%d", &matrix[i * n + j]);
                printf("%3d ", matrix[i * n + j]);
            }
            printf("\n");
        }
     
        fclose(in);
    
        printf("\n");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                printf("%3d ", matrix[i * n + j]);
            printf("\n");
        }
    
        free(matrix);
    
        return 0;
    }

    Here's an example allocating a 2-d array with non-contiguous data (i.e., each row is separately allocated).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define INPUT "ulaz4.txt"
    
    int main() {
        FILE *in = fopen(INPUT, "r");
    
        int n = 0;
        fscanf(in, "%d", &n);
    
        int **matrix = malloc(n * sizeof *matrix);
    
        for (int i = 0; i < n; i++) {
            matrix[i] = malloc(n * sizeof **matrix);
            for (int j = 0; j < n; j++) {
                fscanf(in, "%d", &matrix[i][j]);
                printf("%3d ", matrix[i][j]);
            }
            printf("\n");
        }
     
        fclose(in);
    
        printf("\n");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                printf("%3d ", matrix[i][j]);
            printf("\n");
        }
    
        for (int i = 0; i < n; i++)
            free(matrix[i]);
        free(matrix);
    
        return 0;
    }

    Here's an example allocating a 2-d array with contiguous data (i.e., all rows are allocated together so that they are contiguous).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define INPUT "ulaz4.txt"
    
    int main() {
        FILE *in = fopen(INPUT, "r");
    
        int n = 0;
        fscanf(in, "%d", &n);
    
        // allocate space for matrix row pointers
        int **matrix = malloc(n * sizeof *matrix);
    
        // allocate space for matrix data
        matrix[0] = malloc(n * n * sizeof **matrix);
    
        // set the row pointers
        for (int i = 1; i < n; i++)
            matrix[i] = matrix[i - 1] + n;
    
        // fill the matrix with data
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                fscanf(in, "%d", &matrix[i][j]);
                printf("%3d ", matrix[i][j]);
            }
            printf("\n");
        }
    
        fclose(in);
     
        printf("\n");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                printf("%3d ", matrix[i][j]);
            printf("\n");
        }
    
        free(matrix[0]);  // free matrix data
        free(matrix);     // free matrix row pointers
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. So, this has been bugging me for well over a year now...
    By MutantJohn in forum C Programming
    Replies: 10
    Last Post: 07-12-2014, 09:59 PM
  2. de-bugging problems
    By phantom in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 12:41 AM
  3. Can Anyone Help!! This Is Bugging Me
    By ProgrammingDlux in forum C++ Programming
    Replies: 4
    Last Post: 01-25-2002, 12:21 PM
  4. This error is bugging me!
    By ct28au in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2002, 09:18 AM
  5. de-bugging
    By phantom in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2001, 07:07 PM

Tags for this Thread