简体   繁体   中英

segmentation fault during execution

#include<stdio.h>   
int main()  
{    
    char *arg[10],*c;  
    int count=0;  
    FILE *fp,*fq;  
    printf("Name of the file:");  
    scanf("%s",arg[1]);  
    fp=fopen(arg[1],"w");  
    printf("\t\t%s",arg[1]);  
    printf("Input the text into the file\n");  
    printf("Press Ctrl+d to the stop\n");  
    while((*c=getchar())!=EOF)  
    {  
            fwrite(c,sizeof(char),1,fp);  
            count++;  
    }  
    return 0;  
}  

Change this line

char *arg[10],*c;

to

char arg[1000],c;

This line

scanf("%s",arg[1]);  

to

scanf("%s",arg);  

And this line

while((*c=getchar())!=EOF)

to

while((c=getchar())!=EOF)

Explanation:

char *c; is not a character. It's a pointer to a character. It starts out just pointing to a random bit of memory, which will often be filled with random data - whatever was most recently written there.

char c; is a character.

The same thing applies to char *arg[10] . It's an array of ten pointers. They point into random memory, filled with random data.

Note: my change is not best practice. If someone were to type in a filename 1000 characters or more long, you'd write over the end of the arg buffer. Depending on what you're doing, this can be a security bug.

char *arg[10] ;

arg is array of char pointers. You need to assign them memory locations using malloc before taking input -

scanf("%s",arg[1]); // arg[1] is not assigned to point to any memory location
                    // and is what causing the segmentation fault.

So do -

arg[1] = malloc( stringLengthExpectedToEnter + 1 ) ; // +1 for termination character

Should do like that with the rest of array elements too (or) simply change char*arg[10] to char arg[10] and make sure to enter only enter 9 characters.


I think you are confusing between a pointer and a normal variable .

int *ptr;

ptr is variable that can hold the address of an integer variable. Memory is allocated to for ptr variable to hold an integer address. That's it. ptr is in an uninitalized state and is pointing no where (or) might be pointing to garbage. Dereferencing an uninitialized pointer's behavior is undefined and you are lucky enough if it gives a segmentation-fault .

Now, you need to assign it a valid memory location using malloc .

ptr = malloc( sizeof(int) ) ; // Allocates number of bytes required to hold an
                              // integer and returns it's address.

So, ptr is now pointing to memory location acquired from free store that can hold an integer. Such acquired locations from free stored must be freed using free , else you have classical problem of memory leak . It is good practice to initialize pointer to NULL while declaration.

int *ptr = NULL ;

Hope it helps !

scanf("%d", ptr) ; // Notice that & is not required before ptr. Because ptr 
                   // content is address itself.

A normal variable story is entirely different. When declared -

int var ;

Memory is allocated to var to hold an integer. So, you can directly assign it an integer.

In

char *arg[10];

you define an array of 10 pointers to char but you do not initialize its elements . arg[0], arg[1], ..., arg[9] will all have undefined values.

Then, you try to enter a string into one of those undefined values. Lucky you, you got a segmentation fault. Had you been unlucky, your program could format your hard disk instead.

#include<stdio.h>   
int main()  
{    
   char arg[10],c;  
   int count=0;  
   FILE *fp;  
   printf("Name of the file:");  
   scanf("%s",arg);  
   fp=fopen(arg,"w");  
   printf("\t\t%s",arg);  
   printf("Input the text into the file\n");  
   printf("Press Ctrl+d to the stop\n");  
   while((c=getchar())!=EOF)  
   {  
        fwrite(&c,sizeof(char),1,fp);  
        count++;  
   }

   if(fp != NULL){
      fclose(fp);
      fp = NULL;
   }
   return 0;  
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM