Autumn Strokes Review-c++ (ii)

Source: Internet
Author: User

What is 1.Segmentation fault? What circumstances will cause it to appear? How to solve?

Segmentation fault Chinese is a segment error , in Linux system, the segment error is usually caused by the user program illegally accessing memory (access refers to read, write, or execute), that is, the program accesses memory beyond the allocated memory space of the system, The above error will occur. A segment error is accessing inaccessible memory, which either does not exist or is protected by the system (SIGSEGV).

There may be many reasons for a segment error, Summarized as follows: 1. The program attempts to modify the value of the constant field with a pointer (the constant field is stored in the read-only storage, protected by the operating system, can only read, not write); 2. Accesses memory that is not part of the process address space (the memory in the program is directly accessible to an address, regardless of the address space of the process); is to dereference the null pointer (null); 4. Memory access is out of bounds, the most common is the array access is out of bounds (PS: pointer out of bounds generally do not appear sigsegv, but if the cross-border pointer to dereference will appear SIGCSEGV); 5. Stack overflow (commonly known as stack explosion) will also cause a segment error, the most common is that the number of recursion in the program is too much to cause the stack overflow, causing SIGSEGV; The 6.PRINTF,SPRINTF function prints non-string data as a string (you can see the reason for the source code of printf).

Workaround: Segment error is one of the most common mistakes we have encountered during debugging, but often these problems are not easy to find (most of them are minor problems from the above analysis), and we need to use some tips to help us locate the problem and correct it more efficiently. In general, we can use the following two ways to find out where the problem lies:

(1) Add the printf statement to the key part of the program, print debug information to track the possible location of the segment error in the code in order to facilitate the use of this debugging method, you can #ifdef debug and #endif with conditional compilation instructions to include the printf function, You can view debug information by adding the-ddebug parameter when compiling. Conversely, debugging without this parameter can be done (debugging a small program can be used).

(2) in the Linux platform can use the GNU provided by the GDB tool to implement the program debugging, use is also very convenient, only need to use the gcc/g++ compile with the-G parameter option, and then you can take advantage of GDB add breakpoint, Single step, Printing variable values and other operations to debug the program and the problem of positioning, more suitable for large-scale program debugging (c + + debugging artifact), the efficiency is also good, recommended time to learn how to use.

Specific analysis can refer to this blog: 77775114

Mechanism of 2.fork function and mechanism of vfork function

In a Linux system ,theFork function is used to create a new child process , which has no parameters, there are three cases for the return value, and for the parent process, the fork function returns the process ID of the child process (allocated by the kernel). Returns 0 for the subprocess, and 1 if there is an error, so we can write programs for the corresponding process based on the return value of the fork in the program. The child process created by fork replicates the address space of the parent process, typically if the exec system call is not performed, the child process copies the data and stack segments of the parent process, shares the code snippet of the parent process, and notes that there is no code snippet to replicate the parent process. When the exec system call is executed in a child process, the kernel assigns the address space of the code snippet to the child process, so that after a child process is created, the address space of the child process is completely separate from the parent process, which is two separate processes.

Linux provides a function vfork that functions like the fork function, which is to create a child process that fully shares the address space of the parent process, including the code snippet + data segment + Stack segment . The modification of a child process to a shared resource also affects the parent process. The Vfork function produces a child process that must run first than the parent process. That is, the parent process calls the Vfork function and waits for the child process to run before running.

3.Effective C + + Summary

can refer to this blog: 38553755 (Lite version)

4. Database section

(1) View: Provides the security guarantee of the database, that is, through the view can limit the scope of access to the database, to protect confidential data.

(2) BMS provides DML implementation of data manipulation. DML used in embedded high-level languages is called a host

(3) Attributes in a e-r diagram can be represented as attributes of a relational schema (database logical design)

How does the reciprocal conversion of references and pointers in 5.c++ be implemented?

Reference is a mechanism, not a data type, so there is no reference to the concept of reference and reference pointer, the reference to the address is actually the address of the object pointed to the reference, and the pointer is a variable, its value can be changed, the pointer to a specific variable, the pointer to a reference to the ordinary reference no difference. As an example, the formal parameter of a function is int& a, and we need to pass in a int* p (already initialized) as the actual parameter, we can consider the p to be dereferenced and then passed to the formal parameter, the *p is passed to the function.

6.exec function Family Call

You can refer to this blog: http://blog.sina.com.cn/s/blog_62ef2f1401011wmo.html

7.Linux Memory management mechanism

Linux memory management is mainly divided into kernel space and user space management, the purpose of memory management is to improve the efficiency of memory allocation, reduce the generation of memory fragmentation to improve the use of memory efficiency. For details, refer to this blog:

53366186

Linux memory management is very important, in the interview will often ask, it is recommended to spend more time to understand.

--------------------------------------------------------------------------------------------------------------- ----

The above is my own according to the information on the network, the purpose is to facilitate their own review review, referring to some of the great God's blog, the original text put on the link, thank the great God to share. If there is a mistake, hope the criticism points out.

Autumn Strokes Review-c++ (ii)

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.