C ++ Frequently Asked Questions

Source: Internet
Author: User
  1. Difference between malloc/free and new/delete

    Malloc/free is a standard library function in C language, and new/delete is a C ++ operator.
    Because malloc/free is a library function rather than an operator, it is not controlled by the compiler.
    For custom objects, the use of maloc/free cannot meet the requirements of Dynamic Object Management. (Nonsense)
    New is type-safe, and malloc is not. Int A = new float [2] cannot be compiled. New has built-in sizeof, type conversion, and type security check functions. The return type of malloc is void *, and malloc is only responsible for applying for space.
    For non-Internal data objects, new completes initialization while creating dynamic objects, malloc does not, calloc initializes, and memory is 0. For internal data types, you must add parentheses new to perform initialization. String is a non-Internal data type. Int arr = new int [10]; not initialized, int arr = new int [10] (); initialized to 0.
    New consists of two steps: 1. operator new 2. the first step of calling the constructor is equivalent to the malloc function. However, operator new can be reloaded. You can customize memory allocation policies, or even allocate memory to non-memory devices. malloc is powerless.

  2. What are the advantages of multi-process programming and multi-thread programming?

    After fork, theoretically (without considering the copy-on-write technology), the child process will copy the data space, stack, and heap of the parent process, while the creation thread will not replicate, variables are shared among threads. (Since fork will usually call exec soon, if A Fork sub-process immediately copies the data segment of the parent process, the code segment and Data Segment of the process will be rewritten after the exec sub-process is copied, therefore, the copy-on-write technology is available ).
    For different threads of the same process, the local variables of each thread are private, while global variables, local static variables, and variables allocated to the heap are shared. When accessing these shared variables, If You Want To ensure thread security, you must lock them.

  3. What process should the program go through from the source file to the execution?
     
    2 steps: Compile and link.
    Compile: Convert the source file (. C) to a machine-recognized target binary file (. O ).
    Link: link several target files to generate a complete executable file, in fact, it is the process of converting their various symbol references and symbol definitions into appropriate information in the executable file (generally virtual memory address.
    There is a pre-processing process before official compilation: header file inclusion, macro-defined extension, and Conditional compilation selection.
    Compiling is done by scanning source files multiple times: scanning for lexical analysis at the first time; scanning for syntax analysis at the second time; scanning for code optimization and storage allocation at the third time; perform the fourth scan to generate code.
    Some compilers may generate the target code in an assembly language.
    The link process can be divided into non-static links and dynamic links. Static links are actually completed by GCC calling LD in the background. It loads the linked program to an absolute virtual memory address. A dynamic link is the process of automatically linking the system to call the dynamic linker (ld-linux.so), for example for puts, because it is a dynamic Connection Library libc. so, it will find the address of the puts function in the memory through the dynamic symbolic link when the program is running, so that the program can call this function.
  4. Relationship between thread security and Reentrant

    Reentrant functions must be thread-safe, but they may not be valid in turn.
    Thread safety: a function is called thread safety. It always produces correct results when it is repeatedly called by multiple concurrent threads. Reentrant: the so-called "reentrant". A common situation is that when the program runs to a function Foo (), it receives a signal and suspends the function currently being executed, to the signal processing function, and the execution process of the signal processing function also enters the just-executed function Foo (), so the so-called re-import occurs. In this case, if Foo () can run correctly and the previously paused Foo () can run correctly after processing is completed, it indicates that it can be reentrant.
    To ensure that the function can be reentrant, the following conditions must be met:
    1. Do not use static or global data inside the Function
    2. No static or global data is returned. All data is provided by the function caller.
    3. Use local data or make local copies of global data to protect global data.
    4. Do not call the reentrant function.

  5. Programming a hash table

    Summary

  6. Use mapreduce to multiply a matrix by its transpose

    A × B = C
    The vector product of row I and row J of the original matrix, which forms the element of column J of row I of the result matrix.
    For example, the original matrix is:
    5 7 9
    1 3 6
    Output in mapper stage:
    <(0,-1), (5 7 9)>, <(0, 0), (5 7 9)>, <(1, 0), (5 7 9)>
    <(1,-1), (1 3 6)>, <(0, 1), (1 3 6)>, <(1, 1), (1 3 6)>
    Key. First is the same to the same CER Cer. Key. First indicates the row number in matrix C, and key. Second indicates the column number in matrix C. Key. Second =-1 indicates that the row in matrix A must be multiplied by each row in matrix B.
    Received in cer CER:
    <(0,-1), (5 7 9)>, <(0, 0), (5 7 9)>, <(0, 1 3 6)>
    <(1,-1), (1 3 6)>, <(1, 0), (5 7 9)>, <(1, 1), (1 3 6)>
    Product:
    155.0 80.0
    80.0 46.0

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.