c - free():无效指针中止(核心转储)cs50
问题描述
我的替换密码有问题,我无法正确判断我的代码功能,但它会在我的密文末尾打印额外的感叹号,这是我代码的重要部分
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
明文:你好
密文:vkxxn!
然后我尝试在检查大写的 if 语句之后的循环中添加它,如下所示:
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
明文:你好
密文:vkxxn
free(): 无效指针
中止(核心转储)
对于这个冗长的问题,我深表歉意,并感谢大家的宝贵时间。
解决方案
我找到了导致错误的代码
else if (plaintext[i] = '\0')
{
break;
}
在这里,我将值分配给 '\0' 而不是通过双等号检查值 ==
是什么导致了该错误
free():无效指针
已中止(核心转储)
再次感谢您的时间
推荐阅读
- julia - How does the Julia Type Graph treat arrays?
- python - 请求资源会导致 404 (Flask)
- ios - Fail to open iOS app using Flutter + Firebase
- javascript - 如何在 vue js 中的表单中使用模板
- vhdl - 在modelsim的VHDL测试平台中未检测到endfile,测试平台只是不断重复它自己不确定
- bash - 文件之间的暂停一个一个地复制文件
- image - 无法从 registry.redhat.io 中提取 Red Hat Linux 的 JBoss EAP 7.2 的 Docker 映像
- c# - 在主函数中找不到 appsettings.json 文件?
- javascript - 类型错误:currentTodos.map 不是函数
- php - 作曲家试图分配所有 ram pc