A lot of people tell me that my code results in Memory Leaks. Since, I am still in an academic environment, it has not had any drastic result on any projects, but I still want to correct it.
I have posted below some C code, please go through the coding style , and tell me of any memory leaks.It's a piece of code, which takes some attributes from a text file and makes a graph. But I think the code would be enough to judge my problem.

Assume that getline is a function which reads a line including white spaces and trim(line) is a function which removes those unnecessar trailing characters.

Please tell me what else I should explain for you to help me out.

 int FormGraph()
 {
     fp = fopen("/home/aftab/Desktop/FileContainingTheSemanticAboutInternet.txt","r"); 
     /* MAKING PARENTNODE */
     read = getline(&line, &len, fp);
     line = trim(line);
     strcpy(AllLines[alllin],line);
     ParentNode = (Node*)malloc(200 * sizeof(Node));
     ParentNode->attribute = AllLines[alllin];
     ParentNode->value = AllLines[alllin];
     ParentNode->prev = NULL;

     for(j=0;j<1000;j++) 
       ParentNode->next[j] = NULL;  

     AllAttributes[allatt++] = ParentNode;

     /* FILLING ATTRIBUTES FOR PARENTNODE */
     i = 0;
     while(1)
     {
          read = getline(&line, &len, fp);
          if((ret = strcmp(line = trim(line),"---")) == 0)
             break;

          ptr = (Node*)malloc(sizeof(Node));
          strcpy(AllLines[alllin],line);
          ptr->attribute = AllLines[alllin];
          ptr->value = AllLines[alllin];
          ptr->prev = ParentNode;
          for(j=0;j<1000;j++)
           ptr->next[j] = NULL; 
          AllAttributes[allatt] = ptr;
          ParentNode->next[i] = AllAttributes[allatt];
          alllin++;
          i++;
          allatt++;
      }

      /* FILLING ATTRIBUTES FOR SUB-NODES */
      i = 0;
      while(read = getline(&line, &len, fp) !=-1 )
      {
          line = trim(line);

          for(i=0; i<allatt; i++)
          {
               if(  ((ret = strcmp(AllAttributes[i]->attribute,line)) == 0) && (AllAttributes[i]->next[0] == NULL)   )
               { 
                    while(1)
                    {
                         read = getline(&line, &len, fp); 
                         line = trim(line);

                         if((ret = strcmp(line,"---"))== 0)
                         {
                              i = allatt;
                              break;
                         }
                         else if(read  ==  -1)
                         {
                              printf("\nTotal Attributes = %d",allatt);
                              TotalAttributes = allatt;  
                              i = allatt;
                              break;
                         }
                         else
                         {
                              ptr = (Node*)malloc(200 * sizeof(Node));
                              strcpy(AllLines[alllin],line);
                              ptr->attribute = AllLines[alllin];
                              ptr->value = AllLines[alllin];
                              ptr->prev = AllAttributes[i];

                              for(j=0;j<1000;j++) 
                               ptr->next[j] = NULL;

                              AllAttributes[allatt] = ptr;
                              for(check = 0;check < 1000; check++)
                              {
                                   if(AllAttributes[i]->next[check] == NULL)
                                   {
                                         AllAttributes[i]->next[check] = AllAttributes[allatt];
                                         check = 1000;
                                   }
                              }
                     }
            }
       }
    } 
 }

 fclose(fp);
}

Recommended Answers

All 2 Replies

Please do not leave every second line empty, it makes your code very difficult to read as logical block of code can not be seen with one look.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.