命令行参数分段错误(核心转储)

时间:2014-06-05 23:26:09

标签: c cs50 vigenere

我知道分段错误意味着我正在尝试使用我不应该触摸的内存,但我无法弄清楚它在我的代码中的来源。我为一个使用vigenere的密码加密一些纯文本的作业编写了一个程序。它编译得很好,但是当我使用命令行参数运行时,我会遇到分段错误。

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

int main(int argc, string argv[])
{
// check to make sure the user entered a key
    if(argc != 2)
    {
        printf("You need to enter a key, and only one. Please enter an alphabetical key. \nSyntax: ./vigenere key \n");
        exit (1);
    }

// check to make sure the key is alphabetical
    string k = argv[1];
    if(isalpha(k) == false)
    {
         printf("Pleas enter an alphabetical key.\n");
         exit (2);
    }   
// Get a string of plaintext
    printf("Please enter your secret messege.\n");
    string p = GetString();
// Encipher
    int lk = strlen(k);
    int lp = strlen(p);
    for(int i = 0, j = 0; i < lp; i++, j++)
    {
        if(isupper(k[j]))
        {
            tolower(k[j]);
        }
        if(j > lk)
        {
            j = 0;
        }
        if(isalpha(p[i]))
        {
            if (islower(p[i]))
            {
                printf("%c", ((((p[i] - 97) + (k[j] - 97)) %26) +97));
            }
            else
            {
                printf("%c", ((((p[i] - 65) + (k[j] - 97)) %26) +65));
            }
        }
        else
        {
            printf("%c", p[i]);
        }
    }
    printf("\n");
    return 0;
}

3 个答案:

答案 0 :(得分:3)

此代码看起来很可疑:

if(isupper(k[j]))
    {
        tolower(k[j]);
    }
    if(j > lk)
    {
        j = 0;
    }

你使用k [j],但之后的检查表明j可能大于lk。所以k [j]可能超出范围。

答案 1 :(得分:1)

您的行if(isalpha(k) == false)未检查整个字符串。 isalpha()只接受一个整数参数。

你必须查看整个字符串并为每个字符调用isalpha()。

for( size_t i = 0 ; i < strlen( k ) ; i++ )
{
    if(isalpha(k[i]) == false)
    {
        printf("Pleas enter an alphabetical key.\n");
        exit (2);
    }
} 

答案 2 :(得分:0)

要对此代码进行故障排除,我们需要知道cs50.h中的内容,数据类型“string”的定义方式以及GetString()的内容。分段错误可能发生在以下行:string p = GetString();如果p是一个指针,并且没有分配内存来存储GetString()返回的“字符串”。

如果不包含cs50.h,则将“string”重新定义为字符数组,并将getString()替换为fgets()(以及其他一些小的更改),代码可以正常工作。请参阅下文,并根据您的课程需要进行调整。

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

int main(int argc, char *argv[])
{
    // check to make sure the user entered a key
    if (argc != 2)
    {
        printf("You need to enter a key, and only one. Please enter an alphabetical key. \nSyntax: ./vigenere key \n");
        exit (1);
    }

    // check to make sure the key is alphabetical
    //string k = argv[1];
    char *k = argv[1];
    char *sp;
    for (sp = argv[1]; *sp != '\0'; sp++) {
        if(!isalpha(*sp))
        {
            printf("Pleas enter an alphabetical key.\n");
            exit (2);
        }   
    }

    // Get a string of plaintext
    printf("Please enter your secret message.\n");
    //string p = GetString();
    char p[256];
    fgets(p, 256, stdin);

    // Encipher
    int lk = strlen(k);
    int lp = strlen(p);
    int i;
    int j;
    for(i = 0, j = 0; i < lp; i++, j++)
    {
        if(isupper(k[j]))
        {
            k[j] = tolower(k[j]);
        }
        if(j > lk)
        {
            j = 0;
        }
        if(isalpha(p[i]))
        {
            if (islower(p[i]))
            {
                printf("%c", ((((p[i] - 97) + (k[j] - 97)) %26) +97));
            }
            else
            {
                printf("%c", ((((p[i] - 65) + (k[j] - 97)) %26) +65));
            }
        }
        else
        {
            printf("%c", p[i]);
        }
    }
    printf("\n");
    return 0;
}
相关问题