Thread: Segmentation Fault related to the malloc usage in an iterative loop

  1. #16
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    I was really stressed out that day. Salem helped me out. Thanks. I corrected every single error. I used Salem's advice this way.


    Code:
    while(fgets(line, sizeof(line), pdbfile)) // Loop to find the number of atoms
    {
        if (line[0]== 'A' && line[1] == 'T' && line[2] == 'O' && line[3] == 'M')
        {
            i++;
        }
    }
    
    
    Atom *arr;
    arr = malloc(i*(sizeof(Atom)));
    
    fseek(pdbfile, 0, SEEK_SET);        // Seeking the cursor back to start of file
    k = 0;
    while(fgets(line, sizeof(line), pdbfile))
    {
    if (line[0]== 'A' && line[1] == 'T' && line[2] == 'O' && line[3] == 'M')
        {
            p = line;
            i = 0;
            j = 0;
            flag = 0;
            
            strcpy(arr[k].type,"ATOM");
    
    
            copyField(temp[1],line,7,11);
            arr[k].an = strtol(temp[1],NULL,10);
    
    
            copyField(temp[2],line,13,16);
            strcpy(arr[k].mol,temp[2]);
    
    
            copyField(temp[3],line,18,20);
            strcpy(arr[k].amino,temp[3]);
    
    
            copyField(temp[4],line,22,22);
            strcpy(arr[k].chainid,temp[4]);        
    
    
            copyField(temp[5],line,23,26);
            arr[k].pos=strtol(temp[5],NULL,10);    
    
    
            copyField(temp[6],line,31,38);
            arr[k].xc=strtof(temp[6],&endptr);
    
    
            copyField(temp[7],line,39,46);
            arr[k].yc=strtof(temp[7],&endptr);
    
    
            copyField(temp[8],line,47,54);
            arr[k].zc=strtof(temp[8],&endptr);
    
    
            copyField(temp[9],line,55,60);
            arr[k].occ=strtof(temp[9],&endptr);
    
    
            copyField(temp[10],line,61,66);
            arr[k].temp=strtof(temp[10],&endptr);
    
    
            copyField(temp[11],line,77,78);
            strcpy(arr[k].element,temp[11]);
            //printf("\n%4d %5s %5s %5s %4d %7.3f %7.3f %7.3f %7.3f  %7.3f ",arr[k].an, arr[k].mol, arr[k].amino, arr[k].chainid, arr[k].pos, arr[k].xc, arr[k].yc, arr[k].zc,arr[k].occ,arr[k].temp,arr[k].element);
            k++;
        }
    }
    return arr;
    }
    However, when i tried checking other warnings and clear all the unallocated variables in this big problem 6412 lines, I found the same dynamic memory allocation (Segmentation error) and the warning that pow, sqrt, atan, cos, sin functions are not defined although i have included math.h.
    What could be the reason for it? Does it need to be updated with a new library. If so, how to specifically update that only on this a bit old system.
    Ashish

  2. #17
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Solved Everything here. Thanks..

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Solved Everything here. Thanks..
    You ran valgrind and got zero issues?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this malloc give me a segmentation fault?
    By Xpl0ReRChR in forum C Programming
    Replies: 14
    Last Post: 01-09-2012, 12:05 PM
  2. Replies: 3
    Last Post: 03-12-2011, 08:28 PM
  3. Segmentation fault with Malloc
    By uniprog in forum C Programming
    Replies: 7
    Last Post: 12-02-2010, 10:22 AM
  4. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  5. malloc segmentation fault
    By BharathKumar in forum C Programming
    Replies: 5
    Last Post: 06-27-2007, 02:53 AM

Tags for this Thread