1. How malloc works: malloc uses a data structure (Linked List) to maintain the allocated space
The composition of the linked list: the allocated space, the data of the previous space, the size of the next space, and other information. do not access the space allocated by malloc across borders. it is easy to damage the background maintenance structure. as a result, malloc/free/calloc/realloc is not working properly. 2. about _ stdcall _ cdecl _ fastcall <1>. determines the parameter sequence of function stack pressure stacks. <2>. determines how the function stack is cleared <3>. determines the function name conversion method. _ cdecl is the default function call conversion method of the compiler. It can process Variable Parameter Function calls. The parameter's inbound order is from right to left. After the function is executed, the called function is responsible for clearing the parameters in the stack. During compilation, each function is preceded by an underscore (_) without case-sensitivity conversion. That is, every function called by _ functionname contains the code to clear the stack. Therefore, the size of the executable file generated is larger than that of the function called by _ stdcall. The function uses the stack pressure mode from right to left. Note: For Variable Parameter member functions, always use the _ cdecl conversion method. _ Fastcall: some function call parameters are placed in ECx and EDX, while other parameters are pushed from right to left. The called function is responsible for clearing the parameters in the stack when it is going to return. When embedding an assembly language, you must note the use of registers to avoid conflicts with the compiler. Function Name conversion: @ [email protected] No case-sensitive function name conversion. number indicates the number of bytes of the function parameter. Because some parameters do not need to be written into the stack, this conversion method will increase the function call speed to a certain extent. _ Stdcall function parameters are pushed from right to left into the stack. The called function cleans up the parameters in the stack. Function Name conversion format: [email protected] 3. virtual Memory: When ing a virtual address to a physical address, there is a basic unit: 4 kb Memory Page. if the virtual address is not mapped to a physical address, a segment error occurs when you attempt to access it with a pointer. 4. allocate and release memory function: int BRK (void * End); // allocate space, release space void * sbrk (INT size); // return space address sbrk (INT size) the sbrk and BRK background systems maintain a pointer. the pointer is null by default. call sbrk to determine whether the pointer is 0. Yes: Get the first address initialization pointer of a large idle space. 5. ing virtual memory function: MMAP (Allocation)/unmap (release) void * MMAP (void * Start, // specify the ing virtual address 0 starting from the system specified position) size_t length, // paging space size pagesize multiple int Prot, // ing permission prot_none | prot_r EAD prot_write prot_exec int flags, // int ing method int FD, // file description symbol offset_t off); // ing start position in the file (must be a multiple of pagesize) ing method: memory ing: anonymous ing. File ing: only the last two parameters of the file ing are valid. Map_anonymous map_shared map_private (either) 6. compilation tools and dynamic libraries: <1> gcc-O output file name-o-O0-O1-O2-O3 compilation Optimization-g-G0-G1-G2-G3 generate debugging information-w all error-wall display all warnings-werror warning when error-W close warning-C only compile not to connect-e pre-compile-s Assembly-D define Macros in the command line. Define the macro in the Code and define the macro-X in the command line to specify the language type C ++ C. s none automatic determination-STD = c89 c99 compilation process:-e-c-S Automatic Call connector LD supplement :. c. CPP. cc. h. HPP. o. a. so. I pre-compiled file. s Assembly file <2> 1. compilation process (*. A achieve) 1. 1. compile to the target file-static (optional) GCC-C-static code file. c 1. 2. archive to static library ar tool ar-r |-T ar-r static library file archive file nm tool (view function symbol table) NM static library, dynamic library, target file, or execution file 1. 3. use static library GCC code static library 2. library specifications and conventions library naming rules: Lib library name. a. main version number. minor version number. the name of the Lib library. database A uses rules-database l name-directory of database L <3>. dynamic library compilation 1. what is dynamic Library? (Shared library) the dynamic library can be executed, but the static library cannot be executed, but the dynamic library does not have the main library and cannot be executed independently. The dynamic library is not connected as part of the program. A dynamic library file is required during program execution. 2. the tool LDD can only view executable files in the dynamic library called by the program. readelf-H: Check the execution program header. NM: View function symbols in the database. 3. dynamic library compilation 3. 1. compile-C-FPIC (optional) 3. 2. connect to shared4. use the dynamic library GCC code dynamic library file name GCC code-l library name-l path of the dynamic library standard naming rule: Lib library name. so lib library name. a-l library name-l library path <5> Use libdl. so library dynamic library loading principle function search in dynamic library has been encapsulated into library libdl. so dlopen open a dynamic library dlsym find a function in open dynamic library dlclose close dynamic library // dlerror returned error 7. use of the make tool: (1 ). VI demo. MK (2) EDIT: Demo: gcc-C-FPIC test1.c gcc-C-FPIC test2.c GCC test1.o test2.o-shared-O libtest. so GCC main. c-l test-O main-L. (3 ). run demo. MK file: Make-F demo. MK demo Note: libtest. copy the so library file to the/lib or usr/lib directory 8. use the main method parameter to obtain the environment variable: (ENV can be directly obtained) int main (INT argc, char * argv [], char ** arge) {While (* arge) {printf ("% s \ n", * arge); arge ++ ;}} 9. i/O basics <1>. kernel objects are not allowed to access kernel devices and memory, but can be accessed through kernel system functions. ID of each kernel object. if you access the kernel object, you can only use the ID. programming Model: apply for an ID and use the ID in the kernel system functions to obtain the corresponding Kernel Object Data. <2>. how to access a file using a function, transfer a file, open the file, load the file data, and return an ID. use the function to pass the ID to get the data. use the function to pass the ID to tell the system to release the file. ID: Specifies the description of a file. file description (FD) each program has a directory during execution, storing the opened file description symbol <3>. each program opens three file devices by default: 0: Standard Input 1: Standard Output 2: Error output <4>. operation file description symbol ssize_t write (int fd, void * Buf, // size_t size of the data to be written to the kernel object); // return the size of the written data:> 0 actually written data-1 write error ssize_t read (int fd, void * Buf, // size_t size of the returned data); // return space:> 0: actually read data = 0: encountered file end symbol EOF (CTRL + d)-1: read error suggestion: 0: Input 1: Output 2: Error output <5>. file Type in Linux: directory file D common file F character device file C Block Device File B soft connection file L pipeline file P socket File S
unixc learning summary