C + + memory allocation and copy constructor written examination

Source: Internet
Author: User

Last night to take part in the written test, there are two problems do wrong, are the impression there is a concept, but did not find out how it is going to be, the principle is what, resulting in a wrong problem, now summed up.

A. C + + memory allocation written examination

Review the following questions, please do not look at the answer, see if you can do right, hehe:

What's up, dizzy? The correct answer and analysis are as follows:

Parse: char p[] = "..." is an array, this array is a local variable. Char *p = "..." is a pointer to a string constant. The difference is that the string is in this array, because the array is a local variable (there is a stack area), and when the function is finished, the local variables located in the stack are destroyed, even if the address of the array is returned to the main function, the main function can not access the original string, should output garbled. However, if it is a pointer to a string constant, the string is placed in the program's constant area instead of being placed in a local variable, then the address of the constant is returned to the main function, and the main function can access it.

Here's a summary of memory allocations in C + +:

The memory used by a C + + compiled program is divided into the following sections:

1. Stacking area (Stack)

The release is automatically allocated by the compiler, storing the function address, function parameter value, local variable value, and so on.

2. Heap Area

Is the memory blocks allocated by new, their allocation and release is the responsibility of the programmer, generally a new will correspond to a delete. If the programmer does not release it, the operating system will automatically recycle after the program finishes.

3. Global/static zone (Static)

The storage of global variables and static variables is placed in a block, initializing global variables and initializing static variables in an area, for initializing global variables and uninitialized static variables in another contiguous area. Released by the system after the program is finished.

4. Constant Store (const)

The constant string is put here.

5. Program code Area

Binary code that holds the body of the function.

Among them, the main differences of the stack are as follows:

It's so crooked, let's take a second question ...

Second, examine the copy constructor

The problem is described as follows, asking three cases of the program: A program compilation error, B program compilation success, run-time error, C program normal execution, output 10.

Answer: A

Analysis: The topic examines the relevant knowledge of the copy constructor, in the C + + Primer, the definition of the copy constructor is: Class name (const class name & variable name), that is, the argument is a reference to the class type, but why do we have to define it this way? Of course, the meaning of const is very clear, because the general copy operation does not want to modify the value in the argument, so we use the const to limit it, of course, this const can be removed, namely: a (a &other). Let's examine the necessity of quoting:

1. Recursive call to prevent dead loops

The copy constructor is called in the following cases:

1) An object is passed into the function body in the way of value transfer;

2) An object is returned from the function body in the form of a value transfer;

3) An object needs to be initialized by another object;

Therefore, if the copy constructor is defined as a (a other), then when we use a B = A, it is actually equivalent to passing a as argument to other, and in this case the equivalent of 1) an object passing through the function body as a value, and triggering a call to the copy constructor, And at the call of the next round of replication constructor calls, the key is that this way the call process cannot end, will fall into a dead loop. Therefore, the parameter of the copy constructor must be a reference type.

2. High-efficiency references

References are more efficient, and passing references avoids duplication (which can also be used to explain the previous reason). If a data object is quite large, copying will waste a lot of time, while some types are not supported for replication, such as the IO class can not be copied, passing the reference can avoid these problems.

Through the above discussion we can also draw out the problem of shallow copy and deep copy.

For ordinary types of objects, the copy between them is simple, for example: int a = 10; int b = A; But for objects of class type, in some cases special problems are considered.

Shallow copy: A shallow copy is a simple assignment between the members of an object. For example, when you define a class and do not provide a copy constructor for it, the process of using one of the class's objects to assign a value to another object is a shallow copy. If there are no other resources (such as heaps, files, system resources, etc.) in the object, there is no difference between a deep copy and a shallow copy, but when there are resources in the object, you have to customize the copy constructor to have reasonable control over the objects.

Deep copy: A deep copy refers to when a reference to a Copy object (such as a heap, file, system, and so on) is made (a reference can be a pointer or reference), and the object has to open a new resource instead of simply assigning a pointer or reference to a reference to another resource in the copied object. Such as:

When we use it as follows:

int main ()

{

b b = 10;

b C = b;

return 0;

}

If there is no copy function defined as the red callout, it is a shallow copy, it simply performs a simple member assignment and executes b c = b; The data in both C and B point to the same chunk of memory, and when B performs the destruction, the memory area that it refers to is freed, and C's pointer still points to that area, and a memory leak or program crash occurs when the data pointer is re-accessed or C-refactored. Plus the red callout part is the deep copy, when b C = B, C allocates a copy of the space and copies the values, and when B is finished, c is still accessible because at this point B and C point to two different memory addresses.

Iii. Summary

Through the above questions, found that some of the key points of understanding is not thorough enough, all know what it to examine, but the brain inside the knowledge structure is too chaotic, and ultimately did not come to the correct answer. "C + + Primer" and consolidate it. Come on!!!

C + + memory allocation and copy constructor written examination

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.