linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* unable to free memory details
@ 2002-01-17  6:39 mehul radheshyam choube
  0 siblings, 0 replies; only message in thread
From: mehul radheshyam choube @ 2002-01-17  6:39 UTC (permalink / raw)
  To: kplug-lpsg; +Cc: kplug-newbie, linux-mm, plug-mail

[-- Attachment #1: Type: text/plain, Size: 460 bytes --]


hi friends, 
    here i am giving my c program file(snapshot of the whole program). 
    i am getting a core dump error problem while freeing dynamically allocated memory. 
    the code is big with many functions.i have given the functions along as i format the value contained in the pointer variable(i think must be the problem). 
    if this mail is inconvienence to anybody i am sorry. 
                
    waiting for help. 

mehul.  

[-- Attachment #2: freemem.c --]
[-- Type: application/octet-stream, Size: 12209 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define READ_NO_CHARS       896

#define KEY_VAR_LEN         25

#define VALUE_VAR_LEN       896

#define QSTR_VAR_LEN        2048

#define VALUE_DELIM         "::"

#define LEN_VALUE_DELIM     2

#define NAME_STR_VALUE      "name"

#define NO_OF_FIELDS        4

char            qStr[QSTR_VAR_LEN+1];
char            *dbFieldStr[NO_OF_FIELDS];
char            *dbValStr[NO_OF_FIELDS];

void get_key_val(char *key,char *value,char *buf,char *delim)
{
     strcpy(value,(strstr(buf,delim)+strlen(delim)));

     strcpy(key,strsep(&buf,delim));

}

int remove_spaces(char *buf)
{
    char    *rbuf=NULL;
    int     j=0,i=0,k;

    if((rbuf=(char *)malloc(strlen(buf)+1)) == NULL)
    {
      printf ("ERROR : malloc fail, at line %d of file %s.\n",
               __LINE__, __FILE__);
      return KTS_ERROR;
    }//end of if
    strcpy(rbuf,buf);

    memset(buf,'\0',strlen(buf));

    while(rbuf[i] == ' ')
         i++;

    for(;i<strlen(rbuf);i++)
       buf[j++]=rbuf[i];
    buf[j]='\0';

    while(NULL != strchr(buf,' '))
    {
         if(buf[strlen(buf)-1] != ' ')
           break;
         buf[strlen(buf)-1]='\0';
    }//end of while

    if(rbuf)
      free(rbuf);

    if((rbuf=(char *)malloc(strlen(buf)+1)) == NULL)
    {
      printf ("ERROR : malloc fail, at line %d of file %s.\n",
               __LINE__, __FILE__);
      return KTS_ERROR;
    }//end of if
    strcpy(rbuf,buf);

    for(i=0;rbuf[i]!='\0';i++)
    {
       if((rbuf[i] == ' ' ) && (rbuf[i+1] == ' '))
       {
         k=i-1;
         for(j=i+1;rbuf[j]!='\0';j++,i++)
            rbuf[i]=rbuf[j];
         rbuf[i]='\0';
         i=k;
       }//end of if
    }//end of for
    strcpy(buf,rbuf);

    if(rbuf)
      free(rbuf);

    return 0;
}

int makefirstcap(char *buf)
{
    char    *rbuf=NULL;
    int     i;

    if((strlen(buf)) == 2)
      return 0;

    if((rbuf=(char *)malloc(strlen(buf)+1)) == NULL)
    {
      printf ("ERROR : malloc fail, at line %d of file %s.\n",
               __LINE__, __FILE__);
      return KTS_ERROR;
    }//end of if
    strcpy(rbuf,buf);

    if( !((rbuf[0] >= 'A') && (rbuf[0] <= 'Z')) )
      rbuf[0]=rbuf[0] - ('a' - 'A');

    for(i=1;rbuf[i]!='\0';i++)
    {
       if( !((rbuf[i] >= 'a') && (rbuf[i] <= 'z')) )
         rbuf[i]=rbuf[i] + ('a' - 'A');
    }//end of for
    strcpy(buf,rbuf);

    if(rbuf)
      free(rbuf);

    return 0;
}

int timeformat(char *buf)
{
    char *rbuf=NULL;
    int  i;

    if((rbuf=(char *)malloc(strlen(buf)+1)) == NULL)
    {
      printf ("ERROR : malloc fail, at line %d of file %s.\n",
               __LINE__, __FILE__);
      return KTS_ERROR;
    }//end of if
    strcpy(rbuf,buf);

    for(i=0;rbuf[i]!='\0';i++)
    {
       if(rbuf[i] == '.')
         rbuf[i]=':';
    }//end of for

    if(NULL != (strchr(rbuf,'p')))
    {
      if((rbuf=(char *)realloc(rbuf,strlen(rbuf)+2)) == NULL)
      {
        printf ("ERROR : malloc fail, at line %d of file %s.\n",
                 __LINE__, __FILE__);
        return KTS_ERROR;
      }//end of if
      for(i=0;rbuf[i]!='\0';i++)
      {
         if(rbuf[i] == 'p')
         {
           rbuf[i+1]='m';
           rbuf[i+2]='\0';
         }//end of if(rbuf[i] == 'p')
      }//end of for
    }//end of if(NULL != (strchr(rbuf,'p')))
    strcpy(buf,rbuf);

    if(rbuf)
      free(rbuf);

    return 0;
}

int remove_single_quotes(char *buf)
{
    char    *rbuf=NULL;
    int     j=0,i=0,k;

    printf("%d\n",__LINE__);
    if((rbuf=(char *)malloc(strlen(buf)+1)) == NULL)
    {
      printf ("ERROR : malloc fail, at line %d of file %s.\n",
               __LINE__, __FILE__);
      return KTS_ERROR;
    }//end of if
    printf("%d\n",__LINE__);
    strcpy(rbuf,buf);
    printf("%d\n",__LINE__);

    memset(buf,'\0',strlen(buf));
    printf("%d\n",__LINE__);

    for(i=0;rbuf[i]!='\0';i++)
    {
       if(rbuf[i] == 39)
       {
         k=i-1;
         for(j=i+1;rbuf[j]!='\0';j++,i++)
            rbuf[i]=rbuf[j];
         rbuf[i]='\0';
         i=k;
       }//end of if(rbuf[i] == 39)
    }//end of for
    printf("%d\n",__LINE__);
    strcpy(buf,rbuf);
    printf("%d\n",__LINE__);

    if(rbuf)
      free(rbuf);
    printf("%d\n",__LINE__);

    return 0;
}

void free_memory(void)
{
     int    i;

     for(i=1;i<NO_OF_FIELDS;i++)
     {
        if(dbFieldStr[i])
          free(dbFieldStr[i]);
        if(dbValStr[i])
          free(dbValStr[i]);
     }//end of for
}

int format_name(char *buf)
{
    char    *rbuf=NULL;
    int     i,j=0;

    if((rbuf=(char *)malloc(strlen(buf)+1)) == NULL)
    {
      printf ("ERROR : malloc fail, at line %d of file %s.\n",
               __LINE__, __FILE__);
      return KTS_ERROR;
    }//end of if
    strcpy(rbuf,buf);

    for(i=0;rbuf[i]!='\0';i++)
    {
       if(((rbuf[i] >= 'A') && (rbuf[i] <= 'Z')) || ((rbuf[i] >= 'a') && (rbuf[i] <= 'z')) || (rbuf[i] == ' '))
         buf[j++]=rbuf[i];
    }//end of for
    buf[j]='\0';

    if(rbuf)
      free(rbuf);

    return 0;
}

int main(int argc,char *argv[])
{
    FILE    *fp=NULL;
    char    buf[READ_NO_CHARS+1];
    int     len,pos;
    char    key[KEY_VAR_LEN],value[VALUE_VAR_LEN+1],tvalue[VALUE_VAR_LEN+2];
    char    temp[VALUE_VAR_LEN+1];
    int     isvalue=0,isrec=0;
    int     i;


    if(argc != 3)
    {
      printf("Invalid no. of arguments\n");
      exit(1);
    }//end of if

    if( (fp=fopen(argv[1],"rb")) == NULL)
    {
      printf("Unable to open file %s\n",argv[1]);
      exit(1);
    }//end of if

    if( (dbFieldStr[0]=(char *)malloc(len+1)) == NULL)
    {
      printf ("ERROR : malloc fail, at line %d of file %s.\n",
               __LINE__, __FILE__);
      return KTS_ERROR;
    }//end of if
    strcpy(dbFieldStr[0],"dept_id");
    len=strlen(argv[2]);
    if( (dbValStr[0]=(char *)malloc(len+1)) == NULL)
    {
      printf ("ERROR : malloc fail, at line %d of file %s.\n",
               __LINE__, __FILE__);
      return KTS_ERROR;
    }//end of if
    strcpy(dbValStr[0],argv[2]);

    while(!feof(fp))
    {
         fgets(buf,READ_NO_CHARS,fp);
         buf[strlen(buf)-1]='\0';

         if(eor)  ------------> This is end of record condition(mine one not given here).
         {
           sprintf(qStr,"INSERT INTO dept_schedule(%s,%s,%s,%s) VALUES(%d,%d,%s,%s)",dbFieldStr[0],dbFieldStr[1],dbFieldStr[2],dbFieldStr[3],(int)(atol(dbValStr[0])),(int)(atol(dbValStr[1])),dbValStr[2],dbValStr[3]);
           printf("qStr =%s\n",qStr);
           firesql();
           memset(qStr,'\0',sizeof(qStr));
           printf("%d\n",__LINE__);
           free_memory();-----------> THIS IS THE POINT WHERE CORE DUMP ERROR COMES.
           printf("%d\n",__LINE__);
           continue;
         }//end of if

         if(NULL != (strstr(buf,VALUE_DELIM)))
         {
           strcpy(temp,(strstr(buf,VALUE_DELIM)+LEN_VALUE_DELIM));
           if(0 != strlen(temp))
           {
             isvalue=1;
             get_key_val(key,tvalue,buf,VALUE_DELIM);
             strcpy(value,(strchr(tvalue,' ')+1));

             if((remove_spaces(key)) != 0)
             {
               printf ("ERROR in remove_spaces(), at line %d of file %s.\n",
                        __LINE__, __FILE__);
               return KTS_ERROR;
             }//end of if((remove_spaces(key)) != 0)

             if((remove_spaces(value)) != 0)
             {
               printf ("ERROR in remove_spaces(), at line %d of file %s.\n",
                        __LINE__, __FILE__);
               return KTS_ERROR;
             }//end of if((remove_spaces(key)) != 0)

             if(NULL != (strstr(key,NAME_STR_VALUE)))
             {
               if((format_name(value)) != 0)
               {
                 printf ("ERROR in timeformat(), at line %d of file %s.\n",
                          __LINE__, __FILE__);
                 return KTS_ERROR;
               }//end of if((format_name(value)) != 0)

               if(!strlen(value))
                 isvalue=0;

               if((makefirstcap(value)) != 0)
               {
                 printf ("ERROR in makefirstcap(), at line %d of file %s.\n",
                          __LINE__, __FILE__);
                 return KTS_ERROR;
               }//end of if((makefirstcap(value)) != 0)

               if(strlen(value) == 1)
                 strcat(value,".");

               if(strlen(value) == 1)
                 strcat(value,".");

             }//end of if(NULL != (strstr(key,NAME_STR_VALUE)))
             if(NULL != (strstr(key,TIME_STR_VALUE)))
             {
               if((timeformat(value)) != 0)
               {
                 printf ("ERROR in timeformat(), at line %d of file %s.\n",
                          __LINE__, __FILE__);
                 return KTS_ERROR;
               }//end of if((timeformat(value)) != 0)

             }//end of if(NULL != (strstr(key,TIME_STR_VALUE)))

           }//end of if(0 != strlen(temp))
           else
           {
             isvalue=0;
             buf[strlen(buf)-LEN_VALUE_DELIM]='\0';
             strcpy(key,buf);
             if((remove_spaces(key)) != 0)
             {
               printf ("ERROR in remove_spaces(), at line %d of file %s.\n",
                        __LINE__, __FILE__);
               return KTS_ERROR;
             }//end of if((remove_spaces(key)) != 0)

           }//end of else

         }//end of if(NULL != (strstr(buf,VALUE_DELIM)))
         else
           isvalue=0;

         if(isvalue)
         {
           isrec=1;
           pos=get_key_pos(key);
           if(pos == -1)
             continue;

           len=strlen(key);
           if( (dbFieldStr[pos]=(char *)malloc(len+1)) == NULL)
           {
             printf ("ERROR : malloc fail, at line %d of file %s.\n",
                      __LINE__, __FILE__);
             return KTS_ERROR;
           }//end of if
           strcpy(dbFieldStr[pos],key);

           memset(key,'\0',sizeof(key));

           if((remove_single_quotes(value)) != 0)
           {
             printf ("ERROR in remove_single_quotes(), at line %d of file %s.\n",
                      __LINE__, __FILE__);
             return KTS_ERROR;
           }//end of if((remove_single_quotes(value)) != 0)

           sprintf(tvalue,"'%s'",value);
           strcpy(value,tvalue);

           len=strlen(value);
           if( (dbValStr[pos]=(char *)malloc(len+1)) == NULL)
           {
             printf ("ERROR : malloc fail, at line %d of file %s.\n",
                      __LINE__, __FILE__);
             return KTS_ERROR;
           }//end of if
           strcpy(dbValStr[pos],value);
            pos=-1;
         }//end of if(isvalue)
         else
         {
           isrec=1;
           pos=get_key_pos(key);
           if(pos == -1)
             continue;

           len=strlen(key);
           if( (dbFieldStr[pos]=(char *)malloc(len+1)) == NULL)
           {
             printf ("ERROR : malloc fail, at line %d of file %s.\n",
                      __LINE__, __FILE__);
             return KTS_ERROR;
           }//end of if
           strcpy(dbFieldStr[pos],key);

           memset(key,'\0',sizeof(key));

           if( (dbValStr[pos]=(char *)malloc(7)) == NULL)
           {
             printf ("ERROR : malloc fail, at line %d of file %s.\n",
                      __LINE__, __FILE__);
             return KTS_ERROR;
           }//end of if

           strcpy(dbValStr[pos],"(null)");

           pos=-1;
         }//end of else

         memset(buf,'\0',sizeof(buf));
    }//end of feof(fp) while loop
    if(isrec)
    {
      printf("isrec = %d\n",isrec);
      sprintf(qStr,"INSERT INTO dept_schedule(%s,%s,%s,%s) VALUES(%d,%d,%s,%s)",dbFieldStr[0],dbFieldStr[1],dbFieldStr[2],dbFieldStr[3],(int)(atol(dbValStr[0])),(int)(atol(dbValStr[1])),dbValStr[2],dbValStr[3]);
      printf("qStr =%s\n",qStr);
      firesql();
      printf("%d\n",__LINE__);
      free_memory();
      printf("%d\n",__LINE__);
    }//end of if

    for(i=1;i<NO_OF_FIELDS;i++)
     {
        if(dbFieldStr[i])
          free(dbFieldStr[i]);
        if(dbValStr[i])
          free(dbValStr[i]);
     }//end of for

    if(fp)
      fclose(fp);

    return 0;
}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-01-17  6:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-17  6:39 unable to free memory details mehul radheshyam choube

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox