使用指向 char 的指针时访问冲突写入位置
我正在编写一个非常简单的程序,用于从字符串中删除重复的字符。我运行 Visual Studio 并得到错误:
inteviews.exe 中 0x00d110d9 处出现未处理的异常:0xC0000005:写入位置 0x00d27830 时出现访问冲突。
我真的不明白问题是什么。当前单元格获取下一个单元格的值。
void remove(char *str, char a) {
while (*str != '\0') {
if (*(str+1) == a) {
remove(str + 1, a);
}
*str = *(str +1 );//HERE I GET THE ERROR
++str;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char *str = "abcad";
while (*str != '\0') {
remove(str,*str);
str++;
}
std::cout << str << std::endl;
return 0;
}
编辑:
我已经尝试将其更改为char str[] = "abcad"
,但我仍然遇到相同的错误。
I am writing a very simple program that removes duplicate chars from a string. I ran it visual studio and got the error:
Unhandled exception at 0x00d110d9 in inteviews.exe: 0xC0000005: Access violation writing location 0x00d27830.
I really don't see what the problem is. current cell gets the value of the next cell.
void remove(char *str, char a) {
while (*str != '\0') {
if (*(str+1) == a) {
remove(str + 1, a);
}
*str = *(str +1 );//HERE I GET THE ERROR
++str;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char *str = "abcad";
while (*str != '\0') {
remove(str,*str);
str++;
}
std::cout << str << std::endl;
return 0;
}
EDIT:
I already tried to change it to char str[] = "abcad"
but I still get the same error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在尝试修改字符串文字。你不能那样做。
这是一个字符串文字。它是在只读内存中创建的,因此尝试写入它是访问冲突。
You're attempting to modify a string literal. You can't do that.
That's a string literal. It's created in read-only memory therefore attempting to write to it is an access violation.
一个问题是您创建了一个只读字符串文字并尝试修改它:
您可以改用 char 数组:
One problem is that you created a read-only string literal and attempted to modify it:
You could use a char array instead:
您的程序存在各种各样的问题。我一开始试图把它们全部写下来,但我觉得代码是不可挽回的。它有索引错误、参数传递错误、可疑递归等。
指出尝试修改只读文字的错误的其他答案是正确的。这就是您发布的代码中出现错误的原因。
在我看来,你遇到麻烦的主要原因是,当你只有一个缓冲区时,代码更难编写。您在设计中试图绕过这个限制,但如果使用第二个缓冲区,代码就变得微不足道了。
如果你想变得非常聪明,实际上你可以只用一个缓冲区来轻松管理,只要你保留两个不同的迭代指针。
请注意,我们必须获取缓冲区中第一个字符的副本,该字符将被删除,因为它可能会被迭代修改。
所以现在你回到了开始的地方,只有一个缓冲区。但现在代码可以运行并且很容易理解。
请注意,我的答案是根据您的标签用 C 编写的,但请注意您的代码是 C++。
There are all sorts of problems with your program. I begun by trying to write them all down but I feel that code is irredeemable. It has indexing errors, parameter passing errors, dubious recursion and so on.
The other answers that point out the error of trying to modify a read-only literal are correct. That is the cause of the error in the code you posted.
The main reason for your troubles, in my view, is that the code is harder to write when you only have a single buffer. You have tied yourself in knots trying to get around this limitation in your design, but with a second buffer to work with, the code is trivial.
If you want to get really clever you can in fact manage happily with just a single buffer, so long as you keep two distinct pointers for iteration.
Note that we had to take a copy of first character in the buffer, the character being removed, since that may be modified by the iteration.
So now you are back where you started, with a single buffer. But now the code works and is easy to understand.
Note that my answer is written in C as per your tag, but note that your code is C++.
由于字符串文字是在只读内存中创建的,因此尝试写入它是一种访问冲突。您可以做的是
strcpy(dst, src)
到字符数组。Since string literal is created in read-only memory, attempting to write to it is an access violation. What you can do is
strcpy(dst, src)
to a character array.