Python source code analysis Note 0-C language basics, python source code
Python source code profiling notes 0--C basics Review
To analyze the python source code, the basic knowledge of C language is indispensable, especially pointer and struct. This article first reviews the C language basics to facilitate subsequent code reading.
1. About ELF files
The target and executable files compiled by C in linux are in ELF format. The executable files are divided by segments. In the target file, we divide them by sections. A segment contains one or more sections. You can use the readelf command to view the complete section and segment information. Let's look at a chestnut:
char pear[40];static double peach;int mango = 13;char *str = "hello";static long melon = 2001;int main(){ int i = 3, j; pear[5] = i; peach = 2.0 * mango; return 0;}
This is a simple C language code. Now we can analyze the storage location of each variable. Here, mango and melon belong to the data section, pear and peach belong to the common section, and peach and melon are added with static, which means they can only be used in this file. The string "helloworld" corresponding to str is stored in the rodata section. The main function belongs to the text section. The local variable I in the function allocates space in the stack during runtime. Note that the global uninitialized variables peach and pear are set in the common section for strong and weak symbols. In fact, after the final link becomes an executable file, it will be attributed to BSS segment. Similarly, both the text section and the rodata section belong to the same segment in the executable file.
For more ELF content, see the book "self-cultivation of programmers.
2 pointer
The pointer is the most feared for learning c language in those years. Of course, pointers are well explained in "c and pointer", "C expert programming", and "high quality c programming, let's review the system. Here I have summarized some basic and error-prone points. The environment is ubuntu14.10's 32-bit System and the GCC compilation tool.
2.1 Pointer Error
/*** Example 1 demo1.c ***/int main () {char * str = "helloworld"; // [1] str [1] = 'M '; // [2] char arr [] = "hello"; // [3] arr [1] = 'M'; return 0 ;}
In demo1.c, we define a pointer and an array pointing to a string respectively, and then modify the value of a character in the string. After compilation and running, an error is reported in [2]. Why? Use commandsgcc -S demo1.cAfter the assembly code is generated, the helloworld at [1] is stored in the rodata section and read-only, while that at [3] is stored in the stack. Therefore, [2] reports an error and [3] returns a normal result. In C, A String constant is created in [1] and assigned to the pointer. The String constant is stored in the rodata section. If an array is assigned, it is stored in the stack or data section (for example, [3] is stored in the stack ). Example 2 shows more error-prone points.
/*** Example 2 demo2.c ***/char * GetMemory (int num) {char * p = (char *) malloc (sizeof (char) * num ); return p;} char * GetMemory2 (char * p) {p = (char *) malloc (sizeof (char) * 100);} char * GetString () {char * string = "helloworld"; return string;} char * GetString2 () {char string [] = "helloworld"; return string;} void ParamArray (char a []) {printf ("sizeof (a) = % d \ n", sizeof (a); // sizeof (a) = 4, the parameter is passed as a pointer} int main () {Int a [] = {1, 2, 3, 4}; int * B = a + 1; printf ("delta = % d \ n", B-); // delta = 4, note that the int array step is 4 printf ("sizeof (a) = % d, sizeof (B) = % d \ n", sizeof (), sizeof (B); // sizeof (a) = 16, sizeof (B) = 4 ParamArray (a); // the address that does not belong to the program address space is referenced, cause segment error/* int * p = 0; * p = 17; */char * str = NULL; str = GetMemory (100); strcpy (str, "hello "); free (str); // release the memory str = NULL; // avoid the wild pointer // incorrect version, because the function parameter passes a copy. /* Char * str2 = NULL; GetMemory2 (str2); strcpy (str2, "hello"); */char * str3 = GetString (); printf ("% s \ n", str3); // The stack pointer is returned in an incorrect version, and the compiler generates a warning. /* Char * str4 = GetString2 (); */return 0 ;}
2.2 pointers and Arrays
Some pointers and array contents are also mentioned in 2.1. In C, pointers and arrays can be converted to each other in some cases. For examplechar *str="helloworld"You can usestr[1]To access the second character.*(str+1).
In addition, the use of arrays and pointers in function parameters is equivalent.However, pointers and arrays are not the same in some places, so pay special attention to them.
For example, I define an arraychar a[9] = "abcdefgh";(Note that \ 0 is automatically added after the string), the process for reading the character 'B' with a [1] is as follows:
- First, array a has an address, which is assumed to be 9980.
- Then take the offset value, and the offset value is the index value.Element size. Here the index is 1, and the char size is also 1. Therefore, when 9980 is added to 9981, the address of array a's 1st elements is obtained. (If it is an array of the int type, the offset here is 1.4 = 4)
- The value at address 9981 is 'B '.
If you define a pointerchar *a = "abcdefgh";, We use a [1] to obtain the value of the first element. Different from the array process:
- First, pointer a has its own address, which is assumed to be 4541.
- Then, take the value of a from 4541, that is, the address of the string "abcdefgh", which is assumed to be 5081.
- Next, take the same steps as before. Add offset 1 to 5081 and take the value at the 5082 address. Here it is 'B.
Through the above description, we can find that the pointer has one more step than the array, although it seems that the results are consistent. Therefore, the following error is easy to understand. An array is defined in demo3.c, and then declared and referenced by a pointer in demo4.c. Obviously, an error is returned. If it is changedextern char p[];(Of course, you can also write it as extern char p [3]. It is irrelevant to declare that the array size is different from the actual size ), make sure that the definition and declaration match.
/***demo3.c***/char p[] = "helloworld";/***demo4.c***/extern char *p;int main(){ printf("%c\n", p[1]); return 0;}
3 about typedef and # define
Typedef and # define are often used, but they are different. One typedef can be inserted into multiple declarers, while # define can only have one definition. In the continuous declaration, the type defined by typedef can ensure that all declared variables are of the same type, while # define cannot. In addition, typedef is a type of complete encapsulation, and other types cannot be added after declaration. As shown in the code.
# Define int_ptr int * int_ptr I, j; // I is of the int * type, while j is of the int type. Typedef char * char_ptr; char_ptr c1, c2; // c1, c2 are all char * types. # Define peach intunsigned peach I; // correct typdef int banana; unsigned banana j; // error. The type declared by typedef cannot be extended to other types.
In addition, typedef is also common in struct definition, such as the definition in the following code. Note that [1] and [2] are very different. When you use typedef to define struct foo as in [1], in addition to its own foo structure tag, you also define the structure type of foo, therefore, you can directly declare variables using foo. For example, the definition in [2] cannot use bar to declare variables, because it is only a structure variable, not a structure type.
Another thing to note is that the struct has its own namespace, so the fields in the struct can be the same as the struct name, for example, in [3, of course, try not to use it like this. The subsequent sections will discuss struct in more detail, because many struct are also useful in Python source code.
Typedef struct foo {int I;} foo; // [1] struct bar {int I;} bar; // [2] struct foo f; // correct, use the structure label foofoo f; // correct, use the structure type foostruct bar B; // correct, use the structure label barbar B; // error, use the structure variable bar, bar is already a struct variable and can be directly initialized, such as bar. I = 4; struct foobar {int foorbar;}; // [3] legal definition
4 about struct
Structures are often used to define linked lists and tree structures when learning data structures. For example:
struct node { int data; struct node* next;};
It may be a bit strange when defining the linked list. Why can we define it like this? It seems that the struct node has not been defined yet. Why can we use the next pointer to define it with this struct?
4.1 incomplete type
Here we will talk about the incomplete types in the C language. C can be divided into function type, object type, and incomplete type. Object types can also be classified into scalar type and non-standard type. Arithmetic (such as int, float, char, etc.) and pointer types belong to the scalar type, while fully defined struct, Consortium, arrays, etc. are non-standard data types. The incomplete type means that the complete type is not defined, for example, the following
struct s;union u;char str[];
Variables with incomplete types can be combined into a full type through multiple declarations. For example, the following two words declare that the str array is legal:
char str[];char str[10];
In addition, if two source files define the same variable, as long as not all of them are strongly typed, they can also be compiled. For example, the following is valid,int i;Change to a strong definition, as shown in figureint i = 5;Then an error occurs.
//file1.cint i;//file2.cint i = 4;
4.2 incomplete type struct
Struct of incomplete types is very important. For example, the definition of struct node we mentioned at the beginning, the compiler finds thatstruct node *nextThe struct node is considered to be an incomplete type, and next is a pointer pointing to an incomplete type. However, the pointer itself is completely type, because no matter what pointer is in a 32-bit system, it occupies 4 bytes. By the end of the subsequent definition, struct node becomes a full type, so next is a pointer pointing to a full type.
4.3 struct initialization and size
Struct Initialization is relatively simple. Note that when the struct contains pointers, if you want to copy strings or perform other operations, you need to allocate additional memory space for the pointer. For example, the following defines the stu variable of a struct student and the pstu pointer to the struct. Although the memory of the struct has been implicitly allocated during the stu definition, however, if you want to copy a string to the memory it points to, You need to display the allocated memory.
Struct student {char * name; int age;} stu, * pstu; int main () {stu. age = 13; // correct // strcpy (stu. name, "hello"); // error, name has not allocated memory space stu. name = (char *) malloc (6); strcpy (stu. name, "hello"); // return 0 correctly ;}
The structure size involves an alignment problem. The alignment rules are as follows:
- The first address of the struct variable is the widest member length.
#pragma pack(n), Take the smallest value of the widest member length and n, the integer multiple of the default pragma n = 8)
- The struct size is an integer multiple of the widest member length.
- The offset of each member relative to the first address of the struct is an integer multiple of the size of each member (if pragma pack (n) exists, it is an integer multiple of the size of n and the member ).
Therefore, although the following struct S1 and S2 have the same content, the field order is different and the size is different,Sizeof (S1) = 8, while sizeof (S2) = 12. If you have defined#pragma pack(2), Thensizeof(S1)=8;sizeof(S2)=8
typedef struct node1{ int a; char b; short c;}S1;typedef struct node2{ char b; int a; short c;}S2;
4.4 flexible array
A flexible array refers to an array of unknown size which can be the last member of the struct, so that variable length strings can be stored in the struct. As shown in the code.Note: The flexible array must be the last member of the struct. the flexible array does not occupy the struct size.Of course, you can also write Arrayschar str[0], With the same meaning.
struct flexarray { int len; char str[];} *pfarr;int main(){ char s1[] = "hello, world"; pfarr = malloc(sizeof(struct flexarray) + strlen(s1) + 1); pfarr->len = strlen(s1); strcpy(pfarr->str, s1); printf("%d\n", sizeof(struct flexarray)); // 4 printf("%d\n", pfarr->len); // 12 printf("%s\n", pfarr->str); // hello, world return 0;}
5. Summary
- For const, const in C is not a constant, so the const variable cannot be used to define arrays, as shown in
const int N = 3; int a[N];This is incorrect.
- Pay attention to memory allocation and release to prevent wild pointers.
- Weak symbols and strong symbols in C are valid.
- Note the differences between pointers and arrays.
- Typedef and # define are different.
- Note the initialization of struct containing pointers and the use of flexible arrays.
6 References
- C expert Programming
- Linux c one-stop Programming
- High quality C/C ++ Programming
- Structure byte alignment
- Flexible Array