[LCP]segmentation fault core dump

Andrew Weaver Andrew.Weaver at tecnomen.fi
Sat Feb 2 00:56:50 UTC 2002


Because values is null. 

int line_parser(char **values,char *buf) should be int line_parser(char
***values,char *buf)

and then you must *values=(char **)malloc(iNoofSpaces+1)) == NULL)

Probably easier to have a local version in line_parser and then set *values
= localone.



> -----Original Message-----
> From:	mehul radheshyam choube [SMTP:mehulchoube_cprog at rediffmail.com]
> Sent:	Friday, February 01, 2002 2:50 PM
> To:	linuxcprogramming at lists.linux.org.au
> Subject:	[LCP]segmentation fault core dump
> 
> 
> hi,
> 
>   can any one look into this code please.
> 
> mehul.
> 
> *****************line_parser.c**************************
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> 
> #define KB    1024
> 
> char * remove_spaces(const char *buf)
> {
>     char    *rbuf=(char *)strdup(buf);
>     int     j=0,i=0,k;
> 
>     if(rbuf == NULL)
>       return NULL;
> 
>     for(i=0;rbuf[i]==' ';i++)
>     {
>        for(j=i;rbuf[j]!='\0';j++)
>           rbuf[j]=rbuf[j+1];
>        --i;
>     }//end of for
> 
>     while(strchr(rbuf,' '))
>     {
>          if(rbuf[strlen(rbuf)-1] != ' ')
>            break;
>          rbuf[strlen(rbuf)-1]='\0';
>     }//end of while
> 
>     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
> 
>     return rbuf;
> }
> 
> int line_parser(char **values,char *buf)
> {
>     char    *tmpStr=NULL;
>     int     iNoofSpaces=0,i;
>     char    *value=NULL;
>     char    *tmpBuf=NULL;
>     int     iPosition=0;
>     
>     tmpStr=remove_spaces(buf);
>     if(tmpStr == NULL)
>     {
>       printf ("ERROR in remove_spaces(), at line %d of file %s.\n",
>                __LINE__, __FILE__);
>       return -1;
>     }//end of if(tmpStr == NULL)
>     strcpy(buf,tmpStr);
>     free(tmpStr);
>     tmpStr=NULL;
> 
>     if(buf[strlen(buf)-1] == '\n')
>       buf[strlen(buf)-1] ='\0';
> 
>     if((tmpBuf=(char *)malloc(strlen(buf)+1)) == NULL)
>     {
>       printf ("ERROR : malloc fail, at line %d of file %s.\n",
>               __LINE__, __FILE__);
>       return -1;
>     }//end of if
> 
>     for(i=0;buf[i]!='\0';i++)
>     {
>        if(buf[i] == ' ')
>          iNoofSpaces++;
>     }//end of for
>     printf("iNoofSpaces = %d\n",iNoofSpaces);
> 
>     if((values=(char **)malloc(iNoofSpaces+1)) == NULL)
>     {
>       printf ("ERROR : malloc fail, at line %d of file %s.\n",
>               __LINE__, __FILE__);
>       return -1;
>     }//end of if
> 
>     while(buf!=NULL)
>     {
>          if(strstr(buf," "))
>            strcpy(tmpBuf,(strchr(buf,' ')+1));
>          else
>            tmpBuf=NULL;
>          printf("tmpBuf = %s\n",tmpBuf);
> 
>          for(i=0;buf[i]!=' ' && buf[i]!='\0';i++);
> 
>          if((value=(char *)malloc(i+1)) == NULL)
>          {
>            printf ("ERROR : malloc fail, at line %d of file %s.\n",
>                    __LINE__, __FILE__);
>            return -1;
>          }//end of if
> 
>          for(i=0;buf[i]!=' ' && buf[i]!='\0';value[i]=buf[i],i++);
>          value[i]='\0';
>          printf("value = %s\n",value);
> 
>          if((values[iPosition]=(char *)malloc(strlen(value)+1)) == NULL)
>          {
>            printf ("ERROR : malloc fail, at line %d of file %s.\n",
>                    __LINE__, __FILE__);
>            return -1;
>          }//end of if
>          strcpy(values[iPosition],value);
>          printf("values[iPosition] = %s\n",values[iPosition]);
>          iPosition++;
>          
>          if(value)
>            free(value);
>          value=NULL;
> 
>          if(NULL != tmpBuf)
>          {
>            strcpy(buf,tmpBuf);
>            printf("buf = %s\n",buf);
>          }//end of if(NULL != tmpBuf)
>          else
>            buf=NULL;
>     }//end of while(buf!=NULL)
> 
>     if(tmpBuf)
>       free(tmpBuf);
>     tmpBuf=NULL;
> 
>     return iNoofSpaces+1;
> }
> 
> int main(void)
> {
>     char    str[KB+1];
>     int     ret,i;
>     char    **values=NULL;
> 
>     printf("enter the string :\n");
>     fgets(str,KB,stdin);
> 
>     ret=line_parser(values,str);
> 
>     for(i=0;i<ret;i++)
>        printf("%s\n",values[ret]); /*core dump error comes here*/
> 
>     for(i=0;i<ret;i++)
>        if(values[i])
>        {
>          free(values[i]);
>          values[i]=NULL;
>        }//end of if(values[i])
> 
>     if(values)
>       free(values);
>     values=NULL;
> 
>     return 0;
> }
>  
> 
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List



More information about the linuxCprogramming mailing list