Why cannot the array element of C + + be a reference type __c++

Source: Internet
Author: User

Thank the original author to share: http://blog.xinspace.space/2015/01/25/cpp-array-element-not-ref/


These days while looking at the base content of C + + and seeing arrays, the book mentions the constraints of the array elements:

1. The element type supports assignment operations.

2. The element type supports replication.

Therefore, in addition to referencing type objects and stream objects, any of the built-in types and class types that satisfy the above constraints can become elements of an array.

A Stream object cannot be an array element because it does not support copying and assigning, and does not meet the two constraints mentioned above. But the reference can be assigned and copied Ah, why the array elements can not be a reference.

Let's talk about some of the characteristics of the quote:

1. A reference is another name for the object (where the object refers to the area of memory, such as the memory of int is 4 bytes). Defines a reference to an object that does not occupy the memory space, but is an alias for that object.

As an example:

int i = 10; The statement defines the variable i as an integral type, occupies 4 bytes of memory space, and the initial value is 10. Roughly speaking, this statement in the execution of the operating system from memory allocated 4 bytes (if the address is 0x00000000-0x00000003), put these 4 bytes a label, called I (without the label, want to read and write these 4 bytes can only use binary or hexadecimal address, more trouble). Then, if the program reads and writes I, it represents the 4 bytes of reading and writing. So I is a name or label for these 4-byte objects (this object refers to a specific area of memory, not an object in object orientation). Assigning a value to I will write the value to the 4-byte object. I can easily access an object in memory through I. I do not occupy the memory space itself, I represents the object occupies 4 bytes of space.

int &ir = i; The statement defines the reference IR and binds it to the object represented by I. So, IR is like I is a name, a label, IR represents the same object as I, is 0x00000000-0x00000003 these 4 bytes.

So what's the difference between defining a variable and defining a reference? As you can understand, when defining variables, the operating system helps us allocate the memory space (object) and binds the variable name to the object as the label of the object. While defining a reference, the operating system only creates a label for us, but does not produce an object, and certainly does not bind the tag to an object. Therefore, the work of binding a reference to this tag on an object requires a programmer to do so.

2. References must be bound to the corresponding object. References must be initialized when they are defined, bound to the corresponding objects, and cannot be changed after that.

From the 1th point of view, a programmer must manually bind a reference to an object. This requires that he be provided with an initializer when defining a reference, which represents an object (that is, an area in memory). If the programmer does not do so, then the existence of this tag is meaningless, after all, can not read and write a label (that is, can not read and write the label represents the memory space) in the program does not make any sense, so compile the error.

And the reference cannot be changed after it is bound to an object. This is very well understood: if tag Q can bind to different objects, then the Q in different locations in the program represents which object. is 0x00000000-0x00000003 (4 bytes, such as int) or 0x00000011-0x00000018 (8 bytes, such as Double). Therefore, as long as the object lifecycle is not finished (in the same scope), the label is stuck on it and cannot be used for other objects. Of course, different scopes can have duplicate labels, which is another matter.

again, why can't the elements of an array be references:

1. Large Array initialization trouble. references must be initialized when they are defined. If an array element is a reference, you must initialize each element. such as int a[100000000] Such a large array, the value of the assignment is very troublesome. Even if you can initialize each element, look down.

2. Memory storage continuity that destroys array elements. one of the great advantages of an array is that it can be accessed randomly and quickly, because the array is not only logically contiguous, but also physically continuous. If the array int a[10], assign a value to its 4th element can be written as: * (a+3) = 15;. This can be written because the array elements are stored sequentially in memory, and if the first address of the array A is 0x00000000, then A[0 's address is 0x00000000,a[1] is 0x00000008, and so on. Each element in a occupies 4 bytes of space, each element logically contiguous, that is, the 2nd element is followed by the 1th element, and the storage of the array in physical memory is contiguous, i.e. the address of the 2nd element is next to the 1th element.

So, what happens to an array element as a reference. Referring to the 1th feature mentioned above, references are simply labels that need to be manually bound to an object (address area). When initializing a reference in an array, it is not guaranteed that the object bound by the logically contiguous reference element is also physically contiguous.

As an example:

If the reference array is legitimate and the compilation can be passed, then, according to syntax, the definition statement should be: int &a[4],a is an array with 4 elements and each element is a reference. Of course, we also need to initialize each element, so it's all written:

First, define 4 objects, with memory addresses, labels, and initial values as follows:

int i1 = 10; Memory address is 0x00000000-0x00000003, initial value is 10

int i2 = 11;//memory address is 0x00000014-0x00000017, initial value is 11

int i3 = 12;//memory address is 0x0000000c-0x0000000f, initial value is 12

int i4 = 13;//memory address is 0x00000020-0x00000023, initial value is 13

int &a[4] = {i1, i2, i3, i4};//a 4 elements bound to 4 objects, respectively.

So, since a is an array, then the logical 1th element is next to the 2nd element, and it should be physically so. But this example tells us that the elements of array A are physically discontinuous, the address of the a[0] is the address of the i1, and the address of a[1] is i2 address, and so on. Even the address of the a[2] is more than the address of the a[1] (it should have been done before).

It is because the reference is only alias, does not occupy the memory space, only then causes this problem, makes the array physically discontinuous. In this case, a statement like * (a+3) = 15 is not workable. Because A+3 calculates this: the address of a +3x the number of bytes in the element. The basis for this calculation is to ensure physical continuity, otherwise the address of the remaining elements cannot be launched through the array's first address.

3. The operating system cannot allocate space for reference arrays. object is to occupy memory space, such as int a[10]; This definition statement defines an array of 10 integer elements, so the operating system leaves 40 bytes of memory at run time to array a, and then accesses a to access the 40-byte address. But a reference is not memory-consuming, it's just a name, so if the array element is a reference, such as int &ar[10]={...}, how much memory does this array occupy? I don't know. Because the reference does not occupy space, the operating system cannot allocate memory space to the array and cannot read or write to the array. Does it make sense to have an array that cannot be read or written?

To sum up, the elements of an array cannot be references.

Related Article

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.