为什么在一切都正确完成后,我在应用程序结束时会出现段错误?

发布于 2024-08-28 00:39:50 字数 595 浏览 8 评论 0原文

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
        unsigned char *stole;
        unsigned char pass[] = "m4ak47";
        printf("Vnesi password: \t");
        scanf("%s", stole);
        if(strncmp(stole, pass, sizeof(pass)) != 0)
        {
                printf("wrong password!\n");
                exit(0);
        }
        else
                printf("Password correct\n");
        printf("some stuf here...\n\n");
        return 0;
}

这个程序运行良好,但有一个问题 - 如果密码正确,那么它确实会打印“这里有一些东西......”,但它最后还显示了分段错误错误。为什么 ?

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
        unsigned char *stole;
        unsigned char pass[] = "m4ak47";
        printf("Vnesi password: \t");
        scanf("%s", stole);
        if(strncmp(stole, pass, sizeof(pass)) != 0)
        {
                printf("wrong password!\n");
                exit(0);
        }
        else
                printf("Password correct\n");
        printf("some stuf here...\n\n");
        return 0;
}

This program is working nice, but with one problem - if the password is correct then it DOES do the printing of 'some stuf here...' but it also shows me segmentation fault error at the end. Why ?

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

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

发布评论

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

评论(3

热血少△年 2024-09-04 00:39:50

unsigned char *stole;
上面的语句将 stole 声明为指向 unsigned char 的指针,并包含垃圾值,指向某个随机内存位置。

scanf("%s", 偷);
上面的语句尝试将一些字符串存储到由另一个程序使用的 stole 指向的内存中(至少没有分配给您的程序使用)。因此,当 scanf 尝试覆盖此内存时,您会得到seg-fault

尝试将内存分配给stole,如下所示

unsigned char stole[MAX_SIZE];

unsigned char *stole = malloc((MAX_SIZE+1) * sizeof(char));
// +1 for null-terminating

动态字符串输入

unsigned char *stole;
The above statement declares stole as a pointer to unsigned char and contains garbage value, pointing to some random memory location.

scanf("%s", stole);
The above statement tries to store some string to memory pointed by stole which is being used by another program (atleast not allocated to your program for use). So, when scanf attempts to over-write this memory, you get seg-fault.

Try to allocate memory to stole as follows

unsigned char stole[MAX_SIZE];

or

unsigned char *stole = malloc((MAX_SIZE+1) * sizeof(char));
// +1 for null-terminating

Dynamic String Input

甚是思念 2024-09-04 00:39:50

stole 是一个悬空指针 - 您需要为其分配一些内存(例如 malloc)

stole is a dangling pointer - you need to allocate some memory for it (e.g. malloc)

独留℉清风醉 2024-09-04 00:39:50

您必须为 stole 提供存储空间,例如:

unsigned char stole[1024];

如果用户输入的字符串超过 1024 个字符,这仍然会产生段错误,要解决此问题,您可以使用:

scanf("%1023s", stole);

使用 1023 而不是 1024这样就有空间容纳字符串终止符。

You have to supply the storage for stole, something like:

unsigned char stole[1024];

This will still give a segfault if the user enters a longer string than 1024 chars though, to fix this you can use:

scanf("%1023s", stole);

1023 is used instead of 1024 so that there's room for the string terminator.

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