free():无效的指针异常终止(核心已转储)cs50 [英] free(): invalid pointer Aborted (core dumped) cs50

查看:730
本文介绍了free():无效的指针异常终止(核心已转储)cs50的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在替换密码方面遇到问题,我无法正确确定我的代码功能,但是在我的密文末尾会打印出额外的感叹号,这是代码的重要部分

I'm having problem with substitution cipher that I can't figure out my code functions correctly but it prints extra exclamation mark at the end of my ciphertext here is my important part of the code

int keylen = strlen(argv[1]);
string key = argv[1];
printf("ciphertext: ");
for (int i = 0; i < keylen; i++)
{
    //if it is a lower character we remove the value of a
    //to make it the letters start form 0
    if (islower(plaintext[i]))
    {
        int x = plaintext[i] - 'a';
        printf("%c", tolower(key[x]));
    }
    // the same goes here for uppercase
    else if (isupper(plaintext[i]))
    {
        int y = plaintext[i] - 'A';
        printf("%c", toupper(key[y]));
    }
    else
    {
        //if the text is not an alphabet letter print it as it is
       printf("%c", plaintext[i]);
    }
}

这是输出 〜/pset2/substitution/$ ./subs JTREKYAVOGDXPSNCUIZLFBMWHQ

This is the output ~/pset2/substitution/ $ ./subs JTREKYAVOGDXPSNCUIZLFBMWHQ

纯文本:您好

密文:vkxxn!

然后,我尝试在if语句之后的循环中将其添加为大写形式,如下所示:

Then I tried adding this in the loop after the if statement that checks for uppercase like so:

else if (isupper(plaintext[i]))
{
    int y = plaintext[i] - 'A';
    printf("%c", toupper(key[y]));
}
    // breaks at \0 to prevent printing garbage values
else if (plaintext[i] = '\0')
{
    break;
}

我得到这个奇怪的错误 〜/pset2/substitution/$ ./subs JTREKYAVOGDXPSNCUIZLFBMWHQ

and I get this weird error ~/pset2/substitution/ $ ./subs JTREKYAVOGDXPSNCUIZLFBMWHQ

纯文本:您好

密文:vkxxn

free():无效的指针

free(): invalid pointer

已中止(核心已弃用)

对于这个冗长的问题,我深表歉意,谢谢大家的宝贵时间.

I apologize for this lengthy question and thank you all for your time.

推荐答案

我找到了导致错误的代码

I found the code that makes the error

else if (plaintext[i] = '\0')
{
    break;
}

在这里,我将值分配给'\ 0'而不是通过双等号==
检查该值

Here I'm assigning the value to '\0' instead of checking the value by double equal signs ==
that what gives that error

free():无效的指针
中止(核心已弃用)

free(): invalid pointer
Aborted (core dumped)

再次感谢您的时间

这篇关于free():无效的指针异常终止(核心已转储)cs50的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆