运行 g++ 时出错应用程序。 (字符串加密)
我正在尝试使用 C++ 加密和解密文件,使用以下代码:
#include <iostream>
void crypt(char* pData, unsigned int lenData, const char* pKey, unsigned int lenKey)
{
for (unsigned int i = 0; i < lenData; i++)
pData[i] = pData[i] ^ pKey[i % lenKey];
}
int main()
{
char* data = (char*)"any binary string here";
crypt(data, 22, "key", 3);
std::cout << data;
}
我正在 Windows 6.1 (7) 上使用 g++ (tdm-1) 4.5.1
(MinGW) 进行编译,它使用没有错误或警告。当我尝试运行时,它会显示一个窗口,其中显示“app.exe 停止工作。Windows 可以在线检查是否有解决该问题的方法。” (类似这样,我的 Windows 不是英文的)。我不知道出了什么问题。
I'm trying to encrypt and decrypt files with C++, using this code:
#include <iostream>
void crypt(char* pData, unsigned int lenData, const char* pKey, unsigned int lenKey)
{
for (unsigned int i = 0; i < lenData; i++)
pData[i] = pData[i] ^ pKey[i % lenKey];
}
int main()
{
char* data = (char*)"any binary string here";
crypt(data, 22, "key", 3);
std::cout << data;
}
I'm compiling with g++ (tdm-1) 4.5.1
(MinGW) on Windows 6.1 (Seven), it compiles with no errors or warnings. When I try to run, it shows a window with "app.exe stoped working. The Windows can check online if there has some solution to the problem." (some thing like that, my Windows isn't in English). I don't have any idea about what is wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试修改字符串常量。由于明显的原因(它是恒定的),这是行不通的。相反,请执行以下操作:
You're trying to modify a string constant. For obvious reasons (it's constant), this won't work. Instead, do this:
这句话是错误的:
首先,你不应该使用强制转换。接下来,字符串文字是一个常量。所以应该是:
但是你想覆盖它。所以你需要一个不是常量的字符串。像这样:
This line is wrong:
First, you should not use a cast. Next, a string literal is a constant. So it should be:
But you're wanting to overwrite it. So you need a string that isn't a constant. Like this:
迈克很好地回答了这个问题。您不能修改常量字符串文字。 DOS时代已经快结束了。正确的最新生产级别 C++ 编译器应该发出带有适当标志的警告。只是为了在迈克的答案中添加一点内容,这里是常量字符串文字的一个很好的解释 - http://msdn.microsoft.com/en-us/library/69ze775t(v=vs.80).aspx
另外,这是更好的方法:
注意后自增运算符、
^=
和 sizeof 运算符。对于简单类型,编译器会为你做这种微优化,但养成一个好习惯是件好事。如果您有一个复杂的迭代器,那么使用后增量可能会损害性能关键路径。此外,硬编码字符串的大小很容易出错。稍后您或其他人可能会更改该字符串,但忘记更改其长度。更不用说每次你都得去数字符数。快乐编码!
Mike has answered this question well. You cannot modify constant string literals. Time of DOS has almost ended. Proper up-to-date production level C++ compiler should have issued a warning with appropriate flags. Just to add a little bit to the Mike's answer, here is a good explanation of constant string literals - http://msdn.microsoft.com/en-us/library/69ze775t(v=vs.80).aspx
Also, here is the better way to do it:
Note post-increment operator,
^=
and sizeof operators. For simple types compiler will do this micro-optimization for you, but developing a good habit is good. If you have a complex iterator, using post-increment can harm you on performance critical paths. Also, hardcoding size of strings is error-prone. Later you or someone else can change the string and forget to change its length. Not to mention that every time you have to go and count number of characters.Happy coding!