Thread: segmentation fault

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    12

    segmentation fault

    hello, im having problems whith a pointer to an array of unsigned chars, this is the part of the code that uses it:

    Code:
    
    
    Code:
    unsigned char **matrletras;
    
    CodigoError leer_archivo_txt(FILE* fpIn, unsigned char **Msj, int* nbM){
    int i;
    unsigned char c;
    fseek(fpIn,0,SEEK_END);
    *nbM=ftell(fpIn);
    fseek(fpIn,0,SEEK_SET);
    Msj=malloc((*nbM)*sizeof(int));
    for(i=0; i<(*nbM); i++){
        *(Msj+i)=malloc(sizeof(unsigned char));
        (**(Msj+i))=fgetc(fpIn);
        }
    }


    until that it seems to work fine it saves the character from a file into the array, but when i try to use the array in any function like this:

    Code:
    
    
    Code:
    int holacarola(unsigned char *pa){
    printf("hola");
    }
    
    holacarola(*matrletras);


    the terminal sends ¨segmentation fault¨ error, i have done the same with similar arrays and it works fine, i cant find the mistake help pls.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *nbM is an int, but ftell returns a long int, so this may result in a loss of information:
    Code:
    *nbM=ftell(fpIn);
    This is wrong:
    Code:
    Msj=malloc((*nbM)*sizeof(int));
    Msj is an unsigned char**, and furthermore it is a parameter. This overwrites whatever was passed as an argument, and then gets the allocation wrong by allocation a number of ints instead of a number of unsigned char pointers.

    Looking at your loop, I think that what you want to do is to define the function like this, although I am wary of suggesting that you be a "three-star programmer":
    Code:
    CodigoError leer_archivo_txt(FILE* fpIn, unsigned char ***Msj, long int *nbM) {
        int i;
        fseek(fpIn, 0, SEEK_END);
        *nbM = ftell(fpIn);
        fseek(fpIn, 0, SEEK_SET);
        *Msj = malloc(*nbM * sizeof((*Msj)[0]));
        for (i = 0; i < *nbM; i++) {
            (*Msj)[i] = malloc(sizeof((*Msj)[i][0]));
            (*Msj)[i][0] = fgetc(fpIn);
        }
    }
    but I don't really see the value of dynamically allocating space for just one unsigned char each time in the loop. Perhaps you do want an unsigned char** parameter after all (i.e., you want a dynamic array of unsigned char, but you need a pointer to it so that you can change it from within the function):
    Code:
    CodigoError leer_archivo_txt(FILE* fpIn, unsigned char **Msj, long int *nbM) {
        int i;
        fseek(fpIn, 0, SEEK_END);
        *nbM = ftell(fpIn);
        fseek(fpIn, 0, SEEK_SET);
        *Msj = malloc(*nbM * sizeof((*Msj)[0]));
        for (i = 0; i < *nbM; i++) {
            (*Msj)[i] = fgetc(fpIn);
        }
    }
    Note that you should check if malloc returns a null pointer, and if fgetc returns EOF.
    Last edited by laserlight; 05-05-2020 at 05:14 PM.
    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
    May 2020
    Posts
    12
    Thanks a lot for your help, i will try to apply what you explained. Sadly we are given the type of each variable to use so i cant use a ¨unsigned char***Msj¨ and the second code gives me segmentation fault error right when i call ¨leer_archivo_txt¨ and that didnt happend before, thats the strange thing the error only pops when i use it in other functions.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by stanley1234
    and the second code gives me segmentation fault error right when i call ¨leer_archivo_txt¨ and that didnt happend before
    Then you should debug the function call.
    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
    May 2020
    Posts
    12
    I dont really know what is to debug the function call, but it happens whith any function that i write even if its empty. If i put ¨*matrletras¨ as a variable when i call it segmentation fault comes back, for example:
    Code:
    int hhl(unsigned char *pa){}
    
    hhl(*matrletras);
    

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's because you should write something along the lines of:
    Code:
    FILE *fpIn = fopen(etc, "r");
    unsigned char *matrletras;
    int nBm;
    CodigoError err = leer_archivo_txt(fpIn, &matrletras, &nbM);
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation Fault :(
    By hotshotennis in forum C Programming
    Replies: 2
    Last Post: 05-22-2012, 06:46 PM
  3. Segmentation fault
    By erasm in forum C Programming
    Replies: 1
    Last Post: 10-05-2009, 05:03 PM
  4. Segmentation Fault
    By bigparker in forum C Programming
    Replies: 14
    Last Post: 07-14-2009, 06:55 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread