阅读背景:

我将a值设置为字符串末尾时出现分段错误

来源:互联网 

I wrote a simple function that reverse a string. I try not to allocate a temp char for swap by using the last character '

I wrote a simple function that reverse a string. I try not to allocate a temp char for swap by using the last character '\0' at the end of char*. However, it reports the segmentation fault at *end = *str;

我写了一个简单的函数来反转字符串。我尝试不使用char *末尾的最后一个字符'\ 0'为swap分配临时字符。但是,它会在* end = * str报告分段错误;

Could anyone explain the reason?

谁有人解释原因?

Thank you very much!

非常感谢你!

#include<stdio.h>
#include<stdlib.h>
void reverse(char* str)
{
    char* end = str;
    char* i = str;
    char* j = str;
    while(*end)
        ++end;
    j = end;
    *end = *str;
    while(j > i)
    {
        *i = *j;
        ++i;
        *j = *i;
        ++j;
    }
    while(i < end)
    {
        *i = *(i+1);
        ++i;
    }
    *i = '\0';
}

void main(int argc, char* argv[])
{
    char* str_test1;
    char* str_test2;
    str_test1 = (char*) malloc(10);
    str_test2 = (char*) malloc(2);
    str_test1 = "abcdefjhi";
    str_test2 = "a";
    printf("input str1=%s, str2=%s", str_test1, str_test2);
    reverse(str_test1);
    reverse(str_test2);
    printf("output str=%s, str2=%s", str_test1, str_test2);
    free(str_test1);
    free(str_test2);
}

[After I change the code to the following, it works.]

[将代码更改为以下代码后,它可以正常工作。]

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reverse(char* str)
{
    char* end = str;
    char* i = str;
    char* j = str;
    while(*end)
        ++end;
    j = end;
    *end = *str;
    **--j;**
    while(j > i)
    {
        *i = *j;
        ++i;
        *j = *i;
        **--j;**
    }
    while(i < end)
    {
        *i = *(i+1);
        ++i;
    }
    *i = '\0';
}

void main(int argc, char* argv[])
{
    char* str_test1;
    char* str_test2;
    str_test1 = (char*) malloc(10);
    str_test2 = (char*) malloc(2);
    strcpy(str_test1, "abcdefjhi");
    strcpy(str_test2,"a");
    printf("input str1=%s, str2=%s\n", str_test1, str_test2);
    reverse(str_test1);
    reverse(str_test2);
    printf("output str=%s, str2=%s\n", str_test1, str_test2);
    free(str_test1);
    free(str_test2);
}

1 个解决方案

#1


5  

This does not do what you think it does:

这不符合你的想法:

str_test1 = (char*) malloc(10);
str_test2 = (char*) malloc(2);
str_test1 = "abcdefjhi";
str_test2 = "a";

You probably wanted this:

你可能想要这个:

str_test1 = (char*) malloc(10);
str_test2 = (char*) malloc(2);
strcpy(str_test1, "abcdefjhi");
strcpy(str_test2, "a");

Or this:

char str_test1[10] = "abcdefjhi";
char str_test[2]   = "a";

What you currently have allocates memory, and then overwrites the pointer to the memory you allocated with a pointer to a read-only string literal, and that's a good way to earn a segfault.

你当前有什么分配内存,然后用指向只读字符串文字的指针覆盖指向你分配的内存的指针,这是获得段错误的好方法。

Either of the above replacements will give you a writeable string that you can safely reverse in place.

上述任何一个替换都会为您提供一个可写的字符串,您可以安全地将其反转到位。


' at the end of char*. However, it reports the segmentation fault at *end = *str; I wrote a simple function that reverse a string




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: