"Go" C + + array initialization

Source: Internet
Author: User
Tags array length

If the number of elements in the array initialization list is less than the specified array length, the insufficient element is given the default value.

The original: Some misunderstandings about the initialization of C + + arrays

I used to initialize an array like this and feel good about myself:

int a[50 };    // all initialized to 0

This simple notation made me very happy, so I wanted to initialize the array to 1:

int a[51 };    // I want it all initialized to 1 .

Until 10 minutes ago, I thought the code would actually initialize all 5 elements to 1, but the fact is totally different from what I thought! (The capital of the basic thing revolution, omission not AH)

The line of code that is all initialized to 0 is really no problem and works correctly. The problem is that you want to initialize the array to a number other than 0, that is, a non-default value, which is not feasible (see memory discovery, only the first element of the array is initialized to 1, the others are all 0). This is not because the compiler gave a backdoor to initialize to 0, but because of a basic syntax rule:

If the number of elements in the array initialization list is less than the specified array length, the insufficient element is given the default value.

For a primitive type int, it is, of course, the complement int (), which is 0. Let's look at an array of non-primitive types:

string a[5"foo" };

With the above rules, it is easy to know what is actually equivalent to:
string a[5"foo" "" "" "" ""};

That is, the following 4 elements call the initialization of the string's default constructor, and the first one calls the string::string (const char*).

There is one more difference:

int a[5]; string a[5];

If the initialization list is not explicitly indicated, then the base type is not initialized (except for global variables and static variables), all memory is "dirty", and the class type invokes the default constructor for each element.

Note that in the c++11 the middle of the assignment number can be omitted, that is, int a[5]{1}; Also, if the initialization list is empty, such as int a[5]{}, that will initialize all the elements as default values, i.e., int a[5]{0}; Equivalent

Initialization of a dynamic array after the initialization of the array in the stack, I find that the new array is somewhat different from it:
int New int [5stringnewstring[5intnew int [50stringnewstring[5] Foo " };

The previous lines of code follow the initialization rules for arrays in the stack, and there is a new syntax here:
int New int [5] ();

Note the next pair of parentheses, which means that the entire array is initialized with default values, so for class types, new String[5] is equivalent to new string[5] (), and the default constructor is called to initialize; but for the base type, new Int[5] It is not initialized at all, and new Int[5] () is initialized with the value of int (), which is 0. Seeing this pair of parentheses, I think it's not the argument list for the element's constructor, so I might want to initialize the array to 1:new Int[5] (1); It seems reasonable, but not really. In fact, this pair of parentheses is not a construction parameter of an array element, it may be an entire array, it has three overloaded versions: it looks like a constant reference, an rvalue reference, and a default version. So if you already have an array B of the same size, try to initialize a with B:
int New int [5] (b);
Result compilation error, prompt error C3074:an array cannot is initialized with a parenthesized initializer, it seems that the role of this parenthesis and I think, actually should be seen, If it is initialized with another array then the argument should be const int (&) [5] instead of the const int [5] &amp, and the latter seems to be a wrong type. The question is not solved for the time being. Missed the initialization time (memset) if you want to initialize the array after it is created, you can use the C function memset (), but there is a big problem with the use of the memset, which is that it only works on arrays of type char:
Char a[1//  Set each element to 1

If you replace the a array above with an int or other type, the problem arises because the internal implementation of Memset is assigned in bytes, the int type is greater than one byte (assuming 4), and the array memory is contiguous if the following code is present:
int a[1sizeof(a));


Only the previous sizeof (a) or 40 bytes will be assigned a value of 1 of the operation, that is, the "first 5 int" assignment 0x01010101 operation, near misses great difference! If you really want to initialize again, then honestly loop assignment.

"Go" C + + array initialization

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.