I picked up "C expert programming" and found a new one. This is a "warm and new!
I first read the memory layout of the program, and then looked at the differences between the previous array and pointer. I found that the previous understanding of the C string was vague, and the progress had to be accumulated!
The segment structure and process address control of executable programs are as follows:
These sections will not be introduced. They are everywhere. The following code is written for testing. During the test, the string is generated:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 char* p="we are in text segment"; //text in text segment 5 char globalArr[]="ds"; //text in data segment 6 int globalInt=6; //in data segment 7 int globalIntBSS; //in BSS 8 double globalDoubleBSS; //in BSS 9 char globalArrBSS[10]; //in BSS10 11 int main(void)12 {13 char arr[]="we are in stack!"; //text is in stack14 15 puts("\nfollows are in stack:");16 printf("%p\n",arr);17 printf("%p\n",&arr[1]);18 printf("%p\n",&arr[2]);19 //arr[1]='d';20 21 puts("\nfollows are in BSS:");22 printf("%p\n",&globalIntBSS);23 printf("%p\n",&globalDoubleBSS);24 25 strcpy(globalArrBSS,"lalala");26 printf("%s\n",globalArrBSS);27 printf("%d\n",sizeof(globalArrBSS));28 printf("%p\n",globalArrBSS);29 printf("%p\n",&globalArrBSS[1]);30 31 puts("\nfollows are in data segment:");32 printf("%p\n",&p);33 printf("%p\n",globalArr);34 printf("%p\n",&globalArr[1]);35 printf("%p\n",&globalArr[2]);36 printf("%p\n",&globalInt);37 //globalArr[1]='a';38 39 puts("\nfollows are in text segment:");40 printf("%p\n",p);41 printf("%p\n",&p[1]);42 //p[4]='a';43 44 return 0;45 }
The program result is as follows:
We can see that the memory address varies greatly from top to bottom.
I. the string is in the stack.
Row 3 char arr [] = "we are in stack! "; The defined string is in the stack. Here, arr marks the address of this string, and there is no problem in modifying a character in row 19th, it is readable and writable because it is a variable in the stack.
2. String in BSS
The BSS segment is in the first image of the target file. only variables without values are saved in the out file. In the BSS segment of the target file, only one value indicates the size of the BSS segment in the address space of the process when the program is running, the program is actually running with corresponding variables.
The second line is double globalDoubleBSS. Only one variable is defined and no value is assigned, so it is in the BSS segment. Similarly, the second row of char globalArrBSS [10]; strcpy () is assigned to it during the program running, and their addresses start from 0x601068.
3. the string is in the data segment.
Row 3 char * p = "we are in text segment"; p is in the Data segment because it is a variable that has been initialized, "we are in text segment" is in text segments. Therefore, Row 3 cannot change p [1. In fact, this sentence means: define a global variable p, which is a pointer pointing to the string placed in the text segment, because p has assigned the initial value, so it is placed in the data segment.
The second row of char globalArr [] = "ds"; is different. It is an array in the data segment. It is different from the preceding one because char * p and char p [] are different, I will not talk much about this book. Therefore, Row 3 allows you to change the array.
4. Text segments of strings
Row 3 char * p = "we are in text segment"; this code repeat, there are two points of attention, 1: p is placed in the data segment, because p is initialized data; 2: the string "we are in text segment" is put in the text segment, and it is read-only, therefore, if row 29th is not deregistered, my computer compilation is successful, but a segment error occurs during running, because the read-only cannot be modified, as shown below:
Here we have a different understanding of arrays and pointers. I have explained in detail in Expert C programming. For more information, see the book.