Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

c - Segmentation Fault when writing to a string


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...