Knowledge of memory management, wild pointers, functions for reading and writing characters, pre-compilation, macro definition, and Conditional compilation

Source: Internet
Author: User
Tags fread

1. Memory Management: static allocation and Dynamic Allocation: dynamically allocated as needed during program execution. Static allocation: it is allocated during compilation. Before the program is executed. Memory: code segment, data segment, BSS, stack, heap static allocation, and dynamic allocation are different: static objects are named variables, which are operated directly. A dynamic object is a variable without a name. We operate it indirectly through a pointer. The assignment and release of static objects are automatically handled by the compiler. The assignment and release of dynamic objects must be explicitly managed by the programmer, which is relatively error-prone. 2. Dynamic Memory Allocation: Apply for a dynamic memory space 1. malloc function malloc (in bytes) int * p = (int *) malloc (50); note: to apply for a 0-byte memory, the function does not return NULL, but returns a normal memory address. 2. calloc function calloc (n [how many requests], size is how many bytes); int p [10]; apply for an integer of 10 x p = (int *) calloc (10, sizeof (int); 3. Change the memory space defined in 2 to 100 (pointer, length); p = realloc (p, 100 ); 4. After the application, you can release it and use the free () function. Free (pointer to the dynamic space); cut off the relationship between the pointer variable and the memory. Since then, p has nothing to do with that memory. The space can be reused. 3. memory leaks a dynamically allocated memory. We do not have a pointer to this memory. Therefore, we cannot return it to the program for reuse. Possibility of Memory leakage: 1) re-assigning int * p = (int *) malloc (40); int * q = (int *) malloc (40); p = q; the space originally pointed to by P cannot be returned to the system, which causes leakage. Solution: int * temp; int * p = (int *) malloc (40); temp = p; // store the p address in the Temporary Variable temp int * q = (int *) malloc (40); p = q; then you want to use the original p memory space, p = temp; 2) release the parent level first. Assume that both regions are dynamically applied for and allocated (p and q) and must be manually released. If free (p) is used, memory leakage may occur. the reason is: p points to a new sub-memory space in the memory area. If p is released, the sub-memory space pointed to in the p memory area cannot be released. Solution: first release the sub-memory space free (q), then release the parent space free (p), and 3) return value (new dynamic memory address) int * apply () {return (int *) malloc (20); // return the first address of the newly applied memory space} apply (); // call the return value of the apply () function. No pointer is given. 4. Wild pointer: pointer to junk memory. 1) uninitialized pointer solution: Let the pointer = 0 or = NULL; 2) not assigned NULL after free: Let the pointer = 0 or = NULL; 3) pointer access out-of-bounds solution: control not to cross-border file operations in C Language Concept: a set of data stored on external media simple classification:-program source files. c. obj. exe-a batch of data files obtained from the external program. The C language performs the following operations on the file: (1) Import and Export function stdio. h (2) define the FILE pointer FILE ---- (Special struct, do not declare) (3) open the FILE fopen ([path] FILE name, open mode) path -->. /.. /D: \ temp \ a.txt opening method: r read-only w write only a append r + read/write w + read/write a + append t Open Text File B Open binary file (audio, video, image) (4), file read/write operations (5), close the file pointer fclose (File pointer); 5. Input/output stream input operations Data flows from files to computer memory. Data flows from the computer to the file during the output operation. 6. File Identifier: (1) file path. (2) file name trunk. (3) file suffix. 7. File Buffer concept: the File Buffer system automatically opens a File Buffer for each file in the program in the memory zone. 8. read/write character function: fgetc (fpr); read character from file. Fputc (ch, fpw); writes ch to the file pointed to by fpw. Fgets (str, n, fp); read a string with a length of (n-1) from the file pointed to by fp and store it in the str array. Fputs (str, fp); the string pointed to by str is written into the file pointed to by the file pointer variable fp. Fprintf (File pointer, Format String, output table column); fscanf (File pointer, Format String, input table column); fread (address, number of bytes to read/write, how many data items to read/write, FILE Type pointer); fwrite (address, number of bytes to read and write, number of data items to read and write, FILE type pointer); Note: complete a write operation (fwrite ()) stream must be closed (fclose (); after a read operation (fread () is completed, if the stream is not closed (fclose (), the pointer (FILE * fp) the system automatically moves the length of the previous read/write operation backward. If the stream is not closed, the next read operation is continued, and the last output is continued. 9. Pre-Compilation: refers to the work done before the first scanning (lexical scanning and syntax analysis) of compilation. 10. The Macro command (Macro) file of the pre-compilation and processing command contains the Conditional compilation command (include). These commands all start with "#" to distinguish them from statements. 11. macro definition Classification 1) macro without parameters: # define identifier string e. g: # define PI 3.1415926 # define: macro definition command. # Undef: Terminate the macro definition command. Note: The macro definition is not a C statement and cannot be followed by a semicolon. If a semicolon is added, replace it with the semicolon. A string enclosed in double quotation marks, even if it is the same as the macro name, it is not replaced. 2) a macro with parameters # define macro name (parameter table) string 12. Differences between a macro with parameters and a function 1) processing time: the time when the macro with parameters is compiled. When a function is running. 2) parameter type: the parameter macro has no type problem. Functions are defined parameters and parameter types. 3) processing process: a macro with parameters does not allocate memory, which is a simple replacement of characters. The function is to allocate memory. Evaluate the real parameter value first and then substitute the parameter. 4) program length: the length of the macro with parameters is longer. The function remains unchanged. 5) Running Speed: a macro with parameters does not occupy the running time. Function is the call and return time. 13. Conditional compilation 1. statement format of Conditional compilation: # ifdef identifier segment 1 # else Segment 2 # endif: if the "identifier" has been defined, compile "segment 1 ", otherwise, compile "program segment 2 ". # If expression program segment 1 # else program segment 2 # endif: when the "expression" value is not 0, compile "program segment 1"; otherwise, compile "program segment 2 ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.