执行期间出现分段错误
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将此行更改
为
此行为
并且
此行为
说明
:
char *c;
不是字符。它是一个指向字符的指针。它一开始只是指向内存中的一个随机位,该内存通常会填充随机数据 - 无论最近写入其中的内容。char c;
是一个字符。同样的情况也适用于
char *arg[10]
。它是一个由十个指针组成的数组。它们指向充满随机数据的随机存储器。注意:我的更改不是最佳实践。如果有人要输入 1000 个字符或更长的文件名,您将覆盖
arg
缓冲区的末尾。根据您正在执行的操作,这可能是一个安全错误。Change this line
to
This line
to
And this line
to
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.您
定义了一个由 10 个指向 char 的指针组成的数组,但没有初始化其元素。 arg[0]、arg[1]、...、arg[9] 都将具有未定义的值。
然后,您尝试将字符串输入到这些未定义的值之一中。幸运的是,你遇到了分段错误。如果您运气不好,您的程序可能会格式化您的硬盘。
In
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.
arg
是 char 指针数组。在接受输入之前,您需要使用malloc
为它们分配内存位置 -所以这样做 -
也应该对其余数组元素这样做(或者)简单地更改
char*arg[10] 到
char arg[10]
并确保仅输入 9 个字符。我认为您混淆了指针和普通变量。
ptr
是可以保存整型变量地址的变量。为ptr
变量分配内存以保存整数地址。就是这样。ptr
处于未初始化状态,并且没有指向任何(或)可能指向垃圾的位置。取消引用未初始化指针的行为是未定义的,如果它给出分段错误,那么您就足够幸运了。现在,您需要使用
malloc
为其分配一个有效的内存位置。因此,ptr 现在指向从可以保存整数的自由存储中获取的内存位置。这些从空闲存储中获取的位置必须使用
free
来释放,否则就会遇到内存泄漏的经典问题。在声明时将指针初始化为 NULL 是一个很好的做法。希望有帮助!
普通变量的故事完全不同。声明时 -
内存被分配给 var 来保存整数。因此,您可以直接为其分配一个整数。
arg
is array of char pointers. You need to assign them memory locations usingmalloc
before taking input -So do -
Should do like that with the rest of array elements too (or) simply change
char*arg[10]
tochar arg[10]
and make sure to enter only enter 9 characters.I think you are confusing between a pointer and a normal variable.
ptr
is variable that can hold the address of an integer variable. Memory is allocated to forptr
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
.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 usingfree
, else you have classical problem of memory leak. It is good practice to initialize pointer to NULL while declaration.Hope it helps !
A normal variable story is entirely different. When declared -
Memory is allocated to
var
to hold an integer. So, you can directly assign it an integer.