分段故障(核心倾倒)CS50错误

发布于 2025-02-03 09:31:08 字数 1784 浏览 1 评论 0原文

我一直在编写针对CS50(Caesar)的程序,该程序应该使用命令行中用户给出的密钥来加密消息。该程序应该测试针对许多条件的密钥。我希望我的程序使用返回值在给定的命令行参数无效时停止运行。

下面的代码:(到目前为止,我尚未完成它,仅检查命令行输入)

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int char_repeat(string key);
int key_check(string key);

int main(int argc, string argv[])
{
    string key = argv[1];
    for (int i = 0; i < strlen(key); i++)
    {
        char c = toupper(key[i]);
        // conditions for key (string) written in argv[1]
        if (argc < 2 || strlen(key) != 26 || c < 65 || c > 90)
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }
    }
    char_repeat(key);
    printf("this should not print if invalid...");
    return 0;
}



int char_repeat(string key)
{
    // check for repeated characters in key
    for (int i = 0; i < strlen(key); i++)
    {
        for (int j = i + 1; j < strlen(key); j++)
        {
            if (toupper(key[i]) == toupper(key[j]))
            {
                printf("Usage: ./substitution key\n");
                return 1;
            }
        }
    }
    printf("this should not print if invalid...");
    return 0;
}

当我给出“无效”输入(例如

$ ./sub AB*DEFGHIJKLMNOPQRSTUVWXYZ
$ ./sub ABC
$ ./sub ABCABCABCABCABCABCABCABCAB 
$ ./sub ABCDEFGHIJKLMNOPQRSTUVWXYZ- 

代码)时,通过打印“用法:./substitution key \ n”,请跳过我的测试打印。消息,“如果无效,这不应打印...”。

但是,如果我在不给它的情况下进行第二个论点,这就是问题所在。也就是说,

$ ./sub 

收到一条错误消息

segmentation fault (core dumped) 

我在代码本身中 ,我在其中一个if语句中使用条件(argc&lt; 2),因此它不应该作为所有其他条件并打印“用法:./substitution key \ n “?我知道细分故障是一个错误,试图访问不应访问的内存或其他类型的内存损坏,我只是看不到这与我的程序有何关系。我想这是我使用返回方式的问题。

先感谢您!

I have been writing a program for CS50 (caesar) that is supposed to use a key given by the user in the command line to encrypt a message. The program is supposed to test the key given for a number of conditions. I want my program to use return values to stop running when the command line argument given is invalid.

Code below: (I have not finished it, so far it only checks for the command line input)

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int char_repeat(string key);
int key_check(string key);

int main(int argc, string argv[])
{
    string key = argv[1];
    for (int i = 0; i < strlen(key); i++)
    {
        char c = toupper(key[i]);
        // conditions for key (string) written in argv[1]
        if (argc < 2 || strlen(key) != 26 || c < 65 || c > 90)
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }
    }
    char_repeat(key);
    printf("this should not print if invalid...");
    return 0;
}



int char_repeat(string key)
{
    // check for repeated characters in key
    for (int i = 0; i < strlen(key); i++)
    {
        for (int j = i + 1; j < strlen(key); j++)
        {
            if (toupper(key[i]) == toupper(key[j]))
            {
                printf("Usage: ./substitution key\n");
                return 1;
            }
        }
    }
    printf("this should not print if invalid...");
    return 0;
}

When I give 'invalid' inputs such as

$ ./sub AB*DEFGHIJKLMNOPQRSTUVWXYZ
$ ./sub ABC
$ ./sub ABCABCABCABCABCABCABCABCAB 
$ ./sub ABCDEFGHIJKLMNOPQRSTUVWXYZ- 

The code works perfectly by printing "Usage: ./substitution key\n" skipping my test print message, "this should not print if invalid...".

YET, and this is the problem, if I run ./sub without giving it a second argument. That is,

$ ./sub 

I get an error message

segmentation fault (core dumped) 

In the code itself I use the condition (argc < 2) in one of the if statements so shouldn't it work as all the other conditions and print "Usage: ./substitution key\n" ? I know segmentation fault is an error with trying to access memory that shouldn't be accessed or some other type of memory corruption, I just don't see how this relates to my program. I would imagine it is a problem the way I am using return.

Thank you in advance!

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

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

发布评论

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

评论(1

爱殇璃 2025-02-10 09:31:08

分段故障是试图访问不属于您的内存引起的错误。

当您在不提供其他参数的情况下运行命令./ sub程序中的代码试图访问不存在的参数2时,您无法访问该内存时。

string key = argv[1];

相反,您应该在尝试访问已传递的参数的内存之前检查。

if(argc < 2)
{
    printf("Usage: ./substitution key\n");
    return 1;
}

string key = argv[1];

A segmentation fault is an error caused by attempting to access memory that does not belong to you.

When you run your program without providing additional arguments by using the command ./sub the code in the program tries to access argument 2 which does not exist, so you do not have access to that memory.

string key = argv[1];

Instead, you should check before you try to access that memory that arguments have been passed in.

if(argc < 2)
{
    printf("Usage: ./substitution key\n");
    return 1;
}

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