执行期间出现分段错误

发布于 2024-11-01 15:00:32 字数 480 浏览 7 评论 0原文

#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;  
}  
#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;  
}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半世晨晓 2024-11-08 15:00:33
#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;  
}  
#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;  
}  
绿光 2024-11-08 15:00:32

将此行更改

char *arg[10],*c;

char arg[1000],c;

此行为

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

并且

scanf("%s",arg);  

此行为

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

说明

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

char *c; 不是字符。它是一个指向字符的指针。它一开始只是指向内存中的一个随机位,该内存通常会填充随机数据 - 无论最近写入其中的内容。

char c; 一个字符。

同样的情况也适用于 char *arg[10]。它是一个由十个指针组成的数组。它们指向充满随机数据的随机存储器。

注意:我的更改不是最佳实践。如果有人要输入 1000 个字符或更长的文件名,您将覆盖 arg 缓冲区的末尾。根据您正在执行的操作,这可能是一个安全错误。

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.

醉梦枕江山 2024-11-08 15:00:32

char *arg[10];

定义了一个由 10 个指向 char 的指针组成的数组,但没有初始化其元素。 arg[0]、arg[1]、...、arg[9] 都将具有未定义的值。

然后,您尝试将字符串输入到这些未定义的值之一中。幸运的是,你遇到了分段错误。如果您运气不好,您的程序可能会格式化您的硬盘

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.

夏の忆 2024-11-08 15:00:32
char *arg[10] ;

arg 是 char 指针数组。在接受输入之前,您需要使用 malloc 为它们分配内存位置 -

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

所以这样做 -

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

也应该对其余数组元素这样做(或者)简单地更改 char*arg[10] 到 char arg[10] 并确保仅输入 9 个字符。


我认为您混淆了指针普通变量

int *ptr;

ptr 是可以保存整型变量地址的变量。为ptr 变量分配内存以保存整数地址。就是这样。 ptr 处于未初始化状态,并且没有指向任何(或)可能指向垃圾的位置。取消引用未初始化指针的行为是未定义的,如果它给出分段错误,那么您就足够幸运了。

现在,您需要使用malloc为其分配一个有效的内存位置。

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

因此,ptr 现在指向从可以保存整数的自由存储中获取的内存位置。这些从空闲存储中获取的位置必须使用free来释放,否则就会遇到内存泄漏的经典问题。在声明时将指针初始化为 NULL 是一个很好的做法。

int *ptr = NULL ;

希望有帮助!

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

普通变量的故事完全不同。声明时 -

int var ;

内存被分配给 var 来保存整数。因此,您可以直接为其分配一个整数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文