Why segmentation fault in CODE 1?

141 views Asked by At

Code 1:

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

main(int argc,char **argv)
{
    FILE *fp;
    char lineBuf[100],**p=NULL,temp[100];
    int cnt,j,i;
    if(argc!=2)
    {
        puts("ERROR:Invalid no.of arguments");
        return;
    }
    fp=fopen(argv[1],"r");
    if(fp==NULL)
    {
        printf("'%s' file doesn't exist\n",argv[1]);
    }
    cnt=0;

    while(fgets(lineBuf,100,fp))  //...........loop1
    {
        cnt++;
        p=realloc(p,sizeof(char*)*(cnt));
        if(p==NULL)
        {
            puts("memory unavailable");
            return;
        }

        p[cnt-1]=calloc(1,strlen(lineBuf));
        strcpy(p[cnt-1],lineBuf); //................statement1
    }

    fclose(fp);
    for(i=0;i<cnt;i++)
    {
        for(j=i+1;j<cnt;j++)
        {
            if(strcmp(p[i],p[j])>0)
            {
                strcpy(temp,p[i]);
                strcpy(p[i],p[j]);
                strcpy(p[j],temp);
            }
        }
    }

    fp=fopen(argv[1],"w");
    for(i=0;i<cnt;i++)
        fputs(p[i],fp);
    fclose(fp);
    puts("sorting done");
}

Code 2:

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

main(int argc,char **argv)
{
    FILE *fp;
    char lineBuf[100],**p=NULL,temp[100];
    int cnt,j,i;
    if(argc!=2)
    {
        puts("ERROR:Invalid no.of arguments");
        return;
    }
    fp=fopen(argv[1],"r");
    if(fp==NULL)
    {
        printf("'%s' file doesn't exist\n",argv[1]);
    }
    cnt=0;

    while(fgets(lineBuf,100,fp)) //........loop2
    {
        cnt++;
        p=realloc(p,sizeof(char*)*(cnt));
        if(p==NULL)
        {
            puts("memory unavailable");
            return;
        }

        p[cnt-1]=calloc(1,strlen(lineBuf));

    }
    rewind(fp);

    for(i=0;fgets(lineBuf,100,fp);i++) //........loop3
    {
       strcpy(p[i],lineBuf);//..........statement1
    }
    fclose(fp);
    for(i=0;i<cnt;i++)
    {
        for(j=i+1;j<cnt;j++)
        {
            if(strcmp(p[i],p[j])>0)
            {
                strcpy(temp,p[i]);
                strcpy(p[i],p[j]);
                strcpy(p[j],temp);
            }
        }
    }
    fp=fopen(argv[1],"w");
    for(i=0;i<cnt;i++)
        fputs(p[i],fp);
    fclose(fp);
    puts("sorting done");
}

I have written code for sorting lines in a file. I have copied every line of file to a dynamically allocated memory. In loop 1, if I write statement 1 ,I am getting segmentation fault or memory unavailble. So I have modified and written CODE2. Here I am getting output correctly.

I want to know, what is happening in loop1.

I am fetching data from file and copying in dynamically allocated memory in the same loop. Is it wrong to do allocation and copy at the same time?

3

There are 3 answers

0
Iharob Al Asimi On

I think calloc(1, strlen(lineBuf)) has to be calloc(1, 1 + strlen(lineBuf)), because you are not taking into account the terminating null byte '\0'

0
Gopi On
strcpy(p[i],lineBuf);

i is never initialized in first code snippet

0
inetknght On
fp=fopen(argv[1],"r");
if(fp==NULL)
{
    printf("'%s' file doesn't exist\n",argv[1]);
}
cnt=0;

while(fgets(lineBuf,100,fp)) //........loop2

You check for fp to be NULL but then continue on to use it.