简体   繁体   English

在C中打印char数组会导致分段错误

[英]Printing char array in C causes segmentation fault

I did a lot of searching around for this, couldn't find any question with the same exact issue. 我为此进行了很多搜索,找不到与相同问题完全相同的任何问题。

Here is my code: 这是我的代码:

void fun(char* name){
    printf("%s",name);
}

char name[6];
sscanf(input,"RECTANGLE_SEARCH(%6[A-Za-z0-9])",name)
printf("%s",name);
fun(name);

The name is grabbed from scanf , and it printed out fine at first. 该名称是从scanf抓取的,起初打印得很好。 Then when fun is called, there is a segmentation fault when it tries to print out name. 然后,当调用fun时,尝试打印名称时会出现分段错误。 Why is this? 为什么是这样?

After looking in my scrying-glass, I have it: 看着我的玻璃杯后,我知道了:

Your scanf did overflow the buffer (more than 6 byte including terminator read), with ill-effect slightly delayed due to circumstance: 您的scanf确实溢出了缓冲区(包括终止符读取在内,超过6个字节),由于某些情况,不良影响会稍有延迟:

Nobody else relied on or re-used the memory corrupted at first, thus the first printf seems to work. 最初没有人依赖或重复使用损坏的内存,因此第一个printf似乎可以正常工作。

Somewhere after the first and before the second call to printf the space you overwrote got re-used, so the string you read was no longer terminated before encountering not allocated pages. 在第一次调用printf之后和第二次调用printf之前的某个地方,重复使用了您覆盖的空间,因此,在遇到未分配的页面之前,您读取的字符串不再终止。
Thus, a segmentation-fault at last. 因此,最终出现了分段错误。

Of course, your program was toast the moment it overflowed the buffer, not later when it finally crashed. 当然,您的程序是在缓冲区溢出的那一刻敬酒的,而不是在它最终崩溃时敬酒。
Morale: Never write to memory you have not dedicated for that. 士气:切勿将您不曾为此专用的内容写入内存。

Looking at your edit, the format %6[A-Za-z0-9] tries to read up to 6 characters exclusive the terminator, not inclusive! 查看您的编辑,格式%6[A-Za-z0-9]尝试读取最多6个字符(不包括终结符),不包括终结符!

Since you're reading 6 characters, you have to declare name to be 7 characters, so there's room for the terminating null character: 由于您要读取6个字符,因此必须将name声明为7个字符,因此终止的空字符还有空间:

char name[7];

Otherwise, you'll get a buffer overflow, and the consequences are undefined. 否则,将导致缓冲区溢出,并且后果是不确定的。 Once you have undefined consequences, anything can happen, including 2 successful calls to printf() followed by a segfault when you call another function. 一旦产生不确定的后果,任何事情都可能发生,包括两次成功调用printf()然后在调用另一个函数时出现段错误。

Are you sure that name is zero byte terminated? 您确定name以零字节结尾吗? scanf can overflow your buffer depending on how you are calling it. scanf可能会根据您的调用方式使缓冲区溢出。

If that happens then printf will read beyond the end of the array resulting in undefined behavior and probably a segmentation fault. 如果发生这种情况,则printf将读取数组末尾以外的内容,从而导致未定义的行为以及可能的分段错误。

You're probably walking off the end of the array with your printf statement. 您可能正在使用printf语句离开数组的结尾。 Printf uses the terminating null character '\\0' to know where the end of the string is. Printf使用终止的空字符'\\ 0'来知道字符串的结尾在哪里。 Try allocating your array like this: 尝试像这样分配数组:

char name[6] = {'\0'};

This will allocate your array with every element initially set to the '\\0' character, which means that as long as you don't overwrite the entire array with your scanf, printf will terminate before walking off the end. 这将为数组分配初始设置为'\\ 0'字符的每个元素,这意味着只要您不用scanf覆盖整个数组,printf就会终止,然后结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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