使用指向 char 的指针时访问冲突写入位置

发布于 2024-12-20 19:58:27 字数 745 浏览 0 评论 0原文

我正在编写一个非常简单的程序,用于从字符串中删除重复的字符。我运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灯角 2024-12-27 19:58:27

您正在尝试修改字符串文字。你不能那样做。

char *str = "abcad";

这是一个字符串文字。它是在只读内存中创建的,因此尝试写入它是访问冲突。

You're attempting to modify a string literal. You can't do that.

char *str = "abcad";

That's a string literal. It's created in read-only memory therefore attempting to write to it is an access violation.

缪败 2024-12-27 19:58:27

一个问题是您创建了一个只读字符串文字并尝试修改它:

char *str = "abcad"; // String literals are read-only!

您可以改用 char 数组:

char str[] = "abcad";

One problem is that you created a read-only string literal and attempted to modify it:

char *str = "abcad"; // String literals are read-only!

You could use a char array instead:

char str[] = "abcad";
记忆で 2024-12-27 19:58:27

您的程序存在各种各样的问题。我一开始试图把它们全部写下来,但我觉得代码是不可挽回的。它有索引错误、参数传递错误、可疑递归等。

指出尝试修改只读文字的错误的其他答案是正确的。这就是您发布的代码中出现错误的原因。

在我看来,你遇到麻烦的主要原因是,当你只有一个缓冲区时,代码更难编写。您在设计中试图绕过这个限制,但如果使用第二个缓冲区,代码就变得微不足道了。

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

int main(void)
{
    const char *input = "abcad";
    char *output = malloc(strlen(input)+1);

    char *in = input;
    char *out = output;
    while (*in)
    {
        if (*in != input[0])
        {
            *out = *in;
            out++;
        }
        in++;
    }
    *out = '\0';

    printf("%s\n", output);

    free(output);

    return 0;
}

如果你想变得非常聪明,实际上你可以只用一个缓冲区来轻松管理,只要你保留两个不同的迭代指针。

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

int main(void)
{
    char str[] = "abcad";
    char compare = str[0];

    char *in = str;
    char *out = str;
    while (*in)
    {
        if (*in != compare)
        {
            *out = *in;
            out++;
        }
        in++;
    }
    *out = '\0';

    printf("%s\n", str);

    return 0;
}

请注意,我们必须获取缓冲区中第一个字符的副本,该字符将被删除,因为它可能会被迭代修改。

所以现在你回到了开始的地方,只有一个缓冲区。但现在代码可以运行并且很容易理解。

请注意,我的答案是根据您的标签用 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.

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

int main(void)
{
    const char *input = "abcad";
    char *output = malloc(strlen(input)+1);

    char *in = input;
    char *out = output;
    while (*in)
    {
        if (*in != input[0])
        {
            *out = *in;
            out++;
        }
        in++;
    }
    *out = '\0';

    printf("%s\n", output);

    free(output);

    return 0;
}

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.

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

int main(void)
{
    char str[] = "abcad";
    char compare = str[0];

    char *in = str;
    char *out = str;
    while (*in)
    {
        if (*in != compare)
        {
            *out = *in;
            out++;
        }
        in++;
    }
    *out = '\0';

    printf("%s\n", str);

    return 0;
}

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++.

野の 2024-12-27 19:58:27

由于字符串文字是在只读内存中创建的,因此尝试写入它是一种访问冲突。您可以做的是 strcpy(dst, src) 到字符数组。

#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
    char *str = "abcad";
    char str2[10];
    strcpy(str2, str);
    while (*str2 != '\0') {
        remove(str2, *str2);
        str2++;
    }
}

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.

#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
    char *str = "abcad";
    char str2[10];
    strcpy(str2, str);
    while (*str2 != '\0') {
        remove(str2, *str2);
        str2++;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文