C 中 printf 的不逻辑总线错误

发布于 2024-10-28 04:09:44 字数 813 浏览 1 评论 0原文

当我编译并运行代码时,在打印“开始”后立即出现总线错误。 发生的情况如下:

bash-3.2$ ./remDup
开始
总线错误

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

void removeDups(char* str) 
{
    int len = strlen(str);
    int i = 0;

    for (i = 0; i < len; i++) {
        char a = str[i];
        int k = i + 1;
        int c = 0;
        int j = 0;

        for (j = k; j < len; j++) {
            if (a != str[j]) {
                str[k] = str[j];
                k++;
            } else c++;
        }

        len -= c;
    }

    str[len] = '\0';
}

int main(int argc, const char* argv[] )
{
    char *str1 = "apple";

    printf("%s -> ", str1);
    removeDups(str1);
    printf("%s\n ", str1);

    return 1;
}

When I compile and run my code, I get a bus error right after it prints "starting."
Here is what happens:

bash-3.2$ ./remDup
starting
Bus error

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

void removeDups(char* str) 
{
    int len = strlen(str);
    int i = 0;

    for (i = 0; i < len; i++) {
        char a = str[i];
        int k = i + 1;
        int c = 0;
        int j = 0;

        for (j = k; j < len; j++) {
            if (a != str[j]) {
                str[k] = str[j];
                k++;
            } else c++;
        }

        len -= c;
    }

    str[len] = '\0';
}

int main(int argc, const char* argv[] )
{
    char *str1 = "apple";

    printf("%s -> ", str1);
    removeDups(str1);
    printf("%s\n ", str1);

    return 1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

飞烟轻若梦 2024-11-04 04:09:44

如果您将字符串定义为:

char *str1 = "apple";

您不允许修改内容 - 标准非常清楚这是未定义的行为(a)。使用:

char str1[] = "apple";

相反,它会给你一个可修改的副本。它在功能上等同于:

char str1[6]; strcpy (str1, "apple");

(a) C99 6.4.5“字符串文字”,段落 6 指出:

如果这些数组的元素具有适当的值,则未指定这些数组是否不同。如果程序尝试修改此类数组,则行为未定义。

If you define a string as:

char *str1 = "apple";

you are not permitted to modify the contents - the standard is quite clear that this is undefined behaviour (a). Use:

char str1[] = "apple";

instead and it will give you a modifiable copy. It's functionally equivalent to:

char str1[6]; strcpy (str1, "apple");

(a) C99 6.4.5 "String literals", paragraph 6 states:

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

满意归宿 2024-11-04 04:09:44

您正在修改通常驻留在只读内存中的字符串文字。该标准还规定尝试修改文字是未定义的行为。

当您使用指向字符串文字的指针时,您应该将它们声明为 const、const char * str="text"; 或数组 char str[] = "text";< /code>

更改为例如:

char str1[] = "apple";

在这种情况下,编译器将在堆栈上创建一个数组,并将只读字符串文字复制到其中。

You're modifying string literals which often reside in read-only memory. The standard also states that attempting to modify literals is undefined behavior.

When you're using pointers to string literals, you should either declare them as const, const char * str="text"; or as arrays char str[] = "text";

Change to e.g:

char str1[] = "apple";

In this case the compiler will create an array on stack, and copy the read-only string literal into it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文