简体   繁体   English

在简单的C程序中故障排除段错误

[英]Trouble shoot a seg fault in a simple c program

I am following an example of a book in writing a function to reverse a string in C. This is the following problem. 我正在按照一本书的示例编写一个在C中反转字符串的函数。这是以​​下问题。 But when I execute it under ubuntu using gcc. 但是当我使用gcc在ubuntu下执行它时。 I get a seg fault. 我遇到段错误。 I have tried debugging it, but I don't understand how can this line '*start++ = *end;' 我已经尝试调试它,但是我不明白这行'* start ++ = * end;' causing a seg fault. 导致段故障。

I appreciate someone can help me understand the seg fault. 我感谢有人可以帮助我了解seg错误。

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

void myreverse(char* str) {
    int len = strlen(str);
    char tmp;

    char* start = str;
    char* end = str + (len -1);

    while (start < end) {
        tmp = *start;
        // this is causing Segmentation fault
        *start++ = *end;
        *end-- = tmp;
    }
}

int main(void) {
    char* test = "Hello World";
    puts(test);
    myreverse(test);
    puts(test);
    return EXIT_SUCCESS;
}

String literals are stored in a read only section of your executable. 字符串文字存储在可执行文件的只读部分中。 You could avoid that by changing char* test = "Hello World"; 您可以通过更改char* test = "Hello World";来避免这种情况char* test = "Hello World"; to char test[] = "Hello World"; char test[] = "Hello World"; , where "Hello World" will be copied into the array test. ,其中"Hello World"将被复制到阵列测试中。

Your string buffer is read only memory. 您的字符串缓冲区是只读存储器。 String literals cannot be modified. 字符串文字不能修改。

Use strcpy or strdup to make a buffer of writeable memory. 使用strcpy或strdup创建可写内存的缓冲区。

char* test = strdup("Hello World");

Remember to free the string when you are done with it. 完成操作后,请记住释放该字符串。

char* test = "Hello World";

That is a string literal . 那是一个字符串文字 It's read only; 它是只读的; you can't modify it. 您无法修改。

You are trying to modify it :) 您正在尝试修改它:)

See the answer here: Modifying a C string: access violation 在这里查看答案: 修改C字符串:访问冲突

Basically a C string literal is read only and can't be written to. 基本上,C字符串文字是只读的,不能写入。

char* test = "Hello World";

when you dynamically give a string to a character pointer, the string you give gets saved in Read only part of data segment..Then you are passing this address to your myReverse function where you are trying to change the contents of that location in the line 当您将字符串动态地提供给字符指针时,所提供的字符串将保存在数据段的只读部分中。然后,将该地址传递给myReverse函数,在此您尝试更改行中该位置的内容

*start++ = *end;

which cause the segmentation fault.. 导致分割错误

Just change this line 只需更改此行

char* test = "Hello World";

to

char temp[] = "Hello world";
char *test  = temp;

and it wouldn't crash..See this thread for more details.. 并且它不会crash..See 线程的详细信息..

Based on my experience, seg fault occurs when you are trying to access a memory location outside the scope of your variables. 根据我的经验,当您尝试访问变量范围之外的内存位置时,会发生段错误。 C does not have any bounds checking, so I'm guess somewhere in your code (*start++) is incrementing a pointer to a memory location that you have not reserved in your code. C没有任何边界检查,所以我猜您代码(* start ++)中的某处正在递增指向您未在代码中保留的内存位置的指针。 You can't just iterate through memory locations that you haven't allocated in your code because this could interfere with data that is being used for other purposes and will have unexpected results. 您不能只遍历未在代码中分配的内存位置,因为这可能会干扰用于其他目的的数据,并会产生意外的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM