且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

写入字符串时分段错误

更新时间:2023-11-14 11:35:16

要知道发生了什么,你要明白一个C程序的内存布局。

 的char * s =样品; //这里的样品的字符串被放置在
                        //只读的初始化数据段的内存。

在这里,你不能修改数据。 取值是一个指向一个字符常量(样本)与您试图修改字符常量。这就是为什么你所得到的总线错误错误。

  |主堆栈结构()|
                        |字符* S |
                        | ------------------------------- |
                        |反向栈帧()|
                        |字符*结束|
                        |字符TMP |
                        | |
                        | ------------------------------- |
                        | |
                        | |
                        | |
                        | |
                        | |
                        | ------------------------------- |
                        | |
                        | HEAP |
                        | |
                        | ------------------------------- |
                        | |
                        |未初始化数据(BSS)|
                        | |
                        | ------------------------------- |
                        | |
                        |初始化数据|
                        | |
                        |样品| |
                        | | |
                        |(只读)| (读/写)|
                        | ------------------------------- |
                        |文本或code段|
                        | |
                        | ------------------------------- |

更新
后下是不相关的你的问题。但是,如果你知道在哪里被分配在C所有变量的内存,那么你可以code更好。
下面的程序提供了一个更好地了解C程序的内存布局。
我没有包括图中的命令行参数,函数参数和函数返回值。
谁想要更新这个帖子的人可以添加命令行参数,函数参数和返回的函数值到图中。

  |主堆栈结构()|
| local_To_Main |
| | #包括LT&;&stdio.h中GT;
| ----------------------------------- | #包括LT&;&stdlib.h中GT;
|堆栈功能1帧()| INT gVariable1 = 100;
| local_To_Function1 | INT gVariable2;
| IPTR |字符CString的[10] =你好;
| \\ STACK |字符* CPTR =Hello World的;
| ------ \\ --​​------------- | ------------ |虚空功能1(无效)
| \\ \\ | / | {
| \\ |静态INT J = 5;
| \\ | INT local_To_Function1;
| \\ ^ | INT * I​​PTR;
| \\ | | IPTR =(INT *)malloc的(的sizeof(INT));
| ------------ \\ --​​------------- | ------ |免费(IPTR);
| HEAP \\ --​​- | }
| \\ ---> |国际| |
| --- | INT主要(无效)
| ----------------------------------- | {
| |静态INT I;
|未初始化数据(BSS)| INT local_To_Main;
| gVariable2(初始化为0)|
| I(初始化为0)|
| ----------------------------------- |功能1();
| |返回0;
|初始化数据| }
| |
|的Hello World| gVariable1 = 100 |
| ^ | CString的=你好|
| | | J = 5 |
| | ---< ---< ---- CPTR |
|(只读)| (读/写)|
| ----------------------------------- |
|文本或code段|
| |
| ----------------------------------- |

I am trying to write an in-place reverse function and have followed online code pretty much exactly, yet running the following program throws a bus error. Am I passing the wrong kind of argument to reverse()?

void reverse(char *str) {
    char * end = str;
    char tmp;
    if (str) {
        while (*end) {
            ++end;
        }
        --end;
        while (str < end) {
            tmp = *str;
            *str++ = *end;
            *end-- = tmp;
        }
    }
}

int main() {
    char *s = "sample";
    reverse(s);
    printf("%s\n");
    return 1;
}

To know what is happening, you have to understand the memory layout of a C program.

char *s = "sample";    // Here the "sample" string is placed in 
                        // the read only memory of the Initialized Data segment. 

Here, you cannot modify the data. "s" is a pointer to a char const("sample") and you are trying to modify the char const. That is why you are getting the bus error error.

                        |Stack frame of main()          |
                        |char *s                        |
                        |-------------------------------|
                        |Stack frame of reverse()       |
                        |char *end                      |
                        |char tmp                       |
                        |                               |
                        |-------------------------------|
                        |                               |
                        |                               |
                        |                               |
                        |                               |
                        |                               |
                        |-------------------------------|
                        |                               |
                        |           HEAP                |
                        |                               |
                        |-------------------------------|
                        |                               |
                        |   UNINITIALIZED DATA (BSS)    |
                        |                               |
                        |-------------------------------|
                        |                               |
                        |      INITIALIZED DATA         |
                        |                               |
                        |"sample"   |                   |
                        |           |                   |
                        |(Read Only)| (Read/Write)      |
                        |-------------------------------|
                        |   Text or Code Segment        |
                        |                               |
                        |-------------------------------|

UPDATE Below post is not related to your question. But if you know where are the memory allocated for all the variables in C, then you can code better. The below program gives a better understanding of the memory layout of a C Program. I have not included the command line arguments, function argument and return values of function in the diagram. People who want to update this post can add the command line arguments, function argument and return values of function to the diagram.

|Stack frame of main()              |               
|local_To_Main                      |
|                                   |   #include <stdio.h>
|-----------------------------------|   #include <stdlib.h>
|Stack frame of function1()         |   int gVariable1 = 100;
|local_To_Function1                 |   int gVariable2;
|iptr                               |   char cstring[10] = "Hello";
|     \               STACK         |   char* cptr = "Hello World";
|------\---------------|------------|   void function1(void)
|       \             \|/           |   {
|        \                          |       static int j = 5;
|         \                         |       int local_To_Function1;
|          \                 ^      |       int *iptr;
|           \                |      |       iptr = (int *) malloc(sizeof(int));
|------------\---------------|------|       free(iptr);
|   HEAP      \       ---           |   }
|              \---> |int|          |   
|                     ---           |   int main(void)
|-----------------------------------|   {
|                                   |       static int i;
|   UNINITIALIZED DATA (BSS)        |       int local_To_Main;
|gVariable2(initialized to 0)       |   
|i (initialized to 0)               |
|-----------------------------------|       function1();
|                                   |       return 0;
|      INITIALIZED DATA             |   }
|                                   |
|"Hello World"  |gVariable1 =100    |
|       ^       |cstring="Hello"    |
|       |       |j=5                |
|       |---<---<---- cptr          |
|(Read Only)    | (Read/Write)      |
|-----------------------------------|
|   Text or Code Segment            |
|                                   |
|-----------------------------------|