Thread: Segmentation fault (core dumped)

  1. #1
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72

    Segmentation fault (core dumped)

    Hi again, guys!

    There's a problem with my code and I can't find it... Can u help me?

    Code:
     
    
    void carregaClasses(FILE *g, Classe *classe, int *nClas){
    
    int nAtac, nDefensa, nVida, i;
    char strNom[100], kk;
    
    fscanf(g,"%d", &nClas);
    fscanf(g,"%c", &kk);
    
    classe = (Classe*)malloc(sizeof(Classe) * *nClas);
    if(classe == NULL){
        printf("Error!\n");
    }
    else{
        for(i = 0; i < *nClas; i++){
            fscanf(g,"%s", strNom);
            fscanf(g,"%c",&kk);
            fscanf(g,"%d", &nAtac);
            fscanf(g,"%c", &kk);
            fscanf(g,"%d", &nDefensa);
            fscanf(g,"%c", &kk);
            fscanf(g,"%d",&nVida);
            
            strcopy(strNom, classe[i].strNom);
            classe[i].nAtac = nAtac;
            classe[i].nDefensa = nDefensa;
            classe[i].nVida = nVida;
            fscanf(g,"%c",&kk);  
        }
     }
    }
    While executing the code, the error SEGMENTATION FAULT appears... why? Where?


    Thank you!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    nClass is a pointer to int. The first fscanf() treats that pointer as if it is an int. That is undefined behaviour. In short, remove the ampersand.

    And that's assuming the code which calls the function does what is expected and sane. If the caller does not do what is expected and sane, all bets are even more off.

    Even worse, the change of classe inside your function is not visible to the caller. That will cause a memory leak at best. It will certainly cause a problem if the caller expects the pointer it passes to be allocated - because it won't be.

    There is also no such function as strcopy(), so your code as listed should not even compile.

    Don't even get me started on the fact your code doesn't check that the fscanf() calls succeed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Quote Originally Posted by grumpy View Post
    nClass is a pointer to int. The first fscanf() treats that pointer as if it is an int. That is undefined behaviour. In short, remove the ampersand.

    And that's assuming the code which calls the function does what is expected and sane. If the caller does not do what is expected and sane, all bets are even more off.

    Even worse, the change of classe inside your function is not visible to the caller. That will cause a memory leak at best. It will certainly cause a problem if the caller expects the pointer it passes to be allocated - because it won't be.

    There is also no such function as strcopy(), so your code as listed should not even compile.

    Don't even get me started on the fact your code doesn't check that the fscanf() calls succeed.
    Yeah, that was the problem! Thank you for your complete and fast answer, grumpy! Really appreciate it!

    Another question to you: What do you mean with "There is also no such function as strcopy(), so your code as listed should not even compile."?
    Last edited by catasturslykid; 07-29-2013 at 05:28 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by catasturslykid View Post
    What do you mean with "There is also no such function as strcopy(), so your code as listed should not even compile."?
    What is ambiguous about that statement? There is no function in the standard C library named strcopy().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Quote Originally Posted by grumpy View Post
    What is ambiguous about that statement? There is no function in the standard C library named strcopy().
    Oh, that's true! haha I typed strcopy() while it's strcpy()! Thank you again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped)
    By mrbotts in forum C Programming
    Replies: 2
    Last Post: 01-10-2012, 11:06 AM
  2. Segmentation fault (core dumped)
    By jonagondo in forum C Programming
    Replies: 6
    Last Post: 01-04-2012, 03:56 PM
  3. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  4. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM