0

I'm trying to allocate a multidimensional array to read information from a file and then printing it to check if it is working properly and to get the array information to a 2D array afterwards (not done yet). But for some reason it gives me segfault and returns 139 (0x8B), and when I run it and when I try to debug it it prints everything but at the end it says that it cannot open file: lib-start.c. Thank you in advance!

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>

    int ***getFileLoad (char *nomeFicheiro);
    int ***InitArray (int ***array, int cells);
    void freeArray(int ***array, int cells);

    int main () {

    int ***intArray=NULL, intCells;
    char strNome[10], *p2String=NULL;

    printf("Nome ficheiro: ");
    scanf("%s", strNome);

    p2String=strNome;

    intArray=getFileLoad(p2String);

    intCells=intArray[1][0][0];   /*intCells+2 -> nr of lines to read from the file*/

    freeArray(intArray, intCells);

    return 0;
}

int ***getFileLoad (char *nomeFicheiro) {

    char strLines[5], strColumns[5], strCells[5], strTemp[3];
    int i=0, j=0, k=0, b=2, lines=0, columns=0, cells=0, intTemp=0;
    int ***array=NULL;
    FILE *ficheiro=NULL;

    ficheiro=fopen(nomeFicheiro, "r");   /*open file*/

    if (ficheiro==NULL) {
        printf("Erro ao abrir o ficheiro!\n");
        exit(EXIT_FAILURE);
    }

    fscanf(ficheiro, "%s %s %s", strLines, strColumns, strCells);

    if ((lines=atoi(strLines))<=0) {    
        printf("Invalid nr of lines!\n");
        exit(EXIT_FAILURE);
    }

    if ((columns=atoi(strColumns))<=0) {      
        printf("Invalid nr of columns!\n");
        exit(EXIT_FAILURE);
    }

    if ((cells=atoi(strCells))<=0) {
        printf("Invalid nr of cells!\n");
        exit(EXIT_FAILURE);
    }

    array=InitArray(&array, cells);

    array[0][0][0]=lines;
    array[0][1][0]=columns;
    array[1][0][0]=cells;


    for (i=2 ; i<cells+b ; i++) {       /*nr of lines to read*/
        for (j=0 ; j<1 ; j++) {         /*last value*/
            for (k=0 ; k<4 ; k++) {     /*middle value*/
                if (k==3) {             /*check if there is something else that I don't want on the Array*/
                    intTemp=fgetc(ficheiro);
                    if (intTemp=='\n' || intTemp==EOF) {
                        continue;
                    } else {
                        while(intTemp!='\n' || intTemp!=EOF) {
                            intTemp=fgetc(ficheiro);
                            if (intTemp=='\n' || intTemp==EOF) {
                                break;
                            }
                        }
                    }
                } else {
                    fscanf(ficheiro, "%s", strTemp);

                    if (isdigit(strTemp)) {
                        intTemp=atoi(strTemp);
                        if (k==0) {             /*accepts lines between 1->lines*/
                            if (!(intTemp>=1 && intTemp<lines)) {
                                printf("Invalid values!\n");
                                exit(EXIT_FAILURE);
                            } else {
                            array[i][k][j]=intTemp;
                            }
                        } else if (k==1) {      /*accepts columns between 1->columns*/
                            if (!(intTemp>=1 && intTemp<columns)) {
                                printf("Invalid values!\n");
                                exit(EXIT_FAILURE);
                            } else {
                            array[i][k][j]=intTemp;
                            }
                        } else if (k==2) {      /*accepts cells with values between 0->2*/
                            if (!(intTemp>=0 && intTemp<3)) {
                                printf("Invalid values!\n");
                                exit(EXIT_FAILURE);
                            } else {
                            array[i][k-1][j+1]=intTemp;
                            }
                        }
                    }
                }
            }
        }
    }

    intTemp=fgetc(ficheiro);   /*checking for EOF*/

    if (intTemp!=EOF) {
        printf("EOF not detected!\n");
        exit(EXIT_FAILURE);
    }

    fclose(ficheiro);

    return array;
}

int ***InitArray (int ***array, int cells) {

    int b=2, i=0, j=0, k=0 , l=0;

    array=(int ***)malloc((cells+b)* sizeof(int**));    /*allocating 3D array*/

    if (array==NULL) {
        printf("Not enough memory!\n");
        exit(EXIT_FAILURE);
    }

    for(i=0 ; i<cells+b ; i++) {
        (array)[i]=(int **)malloc(b* sizeof(int*));

        if ((array)[i]==NULL) {
            printf("Not enough memory!\n");
            for (j=0 ; j<i ; j++) {
                free((array)[j]);
            }
            free(array);
            array=NULL;
            exit(EXIT_FAILURE);
        }


        for (j=0 ; j<b ; j++) {
            (array)[i][j]=(int*)malloc(b*sizeof(int));

            if ((array)[i][j]==NULL) {
            printf("Not enough memory!\n");
            for (k=0 ; k<i ; k++) {
                for (l=0 ; l<j ; l++) {
                    free((array)[k][l]);
                }
            }
            free(array);
            array=NULL;
            exit(EXIT_FAILURE);
            }
        }
    }

    return array;
}

void freeArray(int ***array, int cells) {

    int i, j, b=2;

    for (i=0 ; i<cells+b ; i++) {
        for (j=0 ; j<b ; j++) {
            free((array)[i][j]);
        }
        free((array)[i]);
    }
    free(array);
    array=NULL;

}

File example:

22 40
4
1 2 1
1 3 1
1 4 1
1 5 1
10
  • 2
    intArray=getFileLoad(p2String); huh? an int returns to int ***? Jan 13, 2016 at 4:44
  • Please post a Minimal, Complete, and Verifiable example.
    – R Sahu
    Jan 13, 2016 at 5:33
  • @SouravGhosh, my bad... uptading it
    – Slyer
    Jan 13, 2016 at 14:24
  • @RSahu, what do you mean?
    – Slyer
    Jan 13, 2016 at 14:24
  • If you want to return an array of ints, use int * instead of int ***. I'm not even sure what that last one returns. A pointer to a pointer to a pointer to an int? Jan 13, 2016 at 14:39

1 Answer 1

0

The program works fine on my system: Linux Fedora 22 g++ 5.1.1 No SEGFAULT.

I also checked for memory leak and invalid memory access using valgrind

valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all ./StackQuestion

(suppressed: 0 from 0)

==6904== LEAK SUMMARY:
==6904==    definitely lost: 0 bytes in 0 blocks
==6904==    indirectly lost: 0 bytes in 0 blocks
==6904==      possibly lost: 0 bytes in 0 blocks
==6904==    still reachable: 72,704 bytes in 1 blocks
==6904==         suppressed: 0 bytes in 0 blocks
==6904== 
==6904== For counts of detected and suppressed errors, rerun with: -v
==6904== ERROR SUMMARY: 0 errors from 0 contexts

that states that no invalid access have been made and there is just one unharmful memory leak (unharmful because the operating system will reclain that memory correctly on process termination).

Regarding lib_start my guess is that you set up a breakpoint in GDB or you are trying to debug some functions that are basically non-debugable as read() or other function that read/write on files. These function require kernel help for doing this kind of thing (which clearly GDB cannot debug).

I guess we requires more info to reproduce the strange behavior, as g++ verion, OS, and the file you pass to the program.

2
  • Got it to work now! Used Valgrind as you did and it gave me an error on isdigit(strTemp). Thanks man!
    – Slyer
    Jan 14, 2016 at 1:24
  • oh but I tough it was obvious that were 2 errors. isdigit should have been used with stoi and a parameter of initarray is wrong. glad it helped. Please mark the question as answered. Jan 14, 2016 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.