Array initialization in C ++

Source: Internet
Author: User

 

# Include <iostream>
Using STD: cout;
Using STD: Endl;
Int arr1 [5];
Int arr2 [5] = {1, 3, 5 };
Int main ()
{
Int arr3 [5];
Int arr4 [5] = {2, 4, 6 };
Int * PI1 = new int [5];
Int * Pi2 = new int [5] ();
For (INT I = 0; I! = 5; I ++)
Cout <arr1 [I] <"";
Cout <Endl;
For (INT I = 0; I! = 5; I ++)
Cout <arr2 [I] <"";
Cout <Endl;
For (INT I = 0; I! = 5; I ++)
Cout <arr3 [I] <"";
Cout <Endl;
For (INT I = 0; I! = 5; I ++)
Cout <arr4 [I] <"";
Cout <Endl;
For (INT I = 0; I! = 5; I ++)
Cout <* (PI1 + I) <"";
Cout <Endl;
For (INT I = 0; I! = 5; I ++)
Cout <* (Pi2 + I) <"";
Cout <Endl;
Return 0;
}

Results of Visual Studio 2005 compiling and running in Windows XP SP3:

Results of compiling and running eclipse + CDT + mingw in Windows XP SP3:

In Ubuntu Linux 8.04, eclipse + CDT + G ++ compiled and run:

The initialization results of the first four arrays in the above three results are the same. The following conclusions are obtained:

For arrays with built-in elements

Arr1: static allocation, defined in the function body, explicitly initialized without the initialization list, all elements are initialized to 0;

Arr2: static allocation, defined in the function in vitro, and initialized to 0 for elements that do not provide initial values in the initialization list;

Arr3: static allocation, defined in the function body, and explicitly initialized without the initialization list. elements are not initialized;

Arr4: static allocation, defined in the function body, initialization of elements that do not provide initial values in the initialization list is 0;

The initialization results of the last two arrays in the three results seem to depend on the compiler, and the results of mingw and G ++ are actually different (the results of mingw are too strange ). The initialization results of the first group (MS compiler) are consistent with those of the Dynamic Allocation array initialization rules described in C ++ primer (version 4), that is, when arrays are dynamically allocated, if the array element is of the built-in type, no Initialization is performed, for example, pi1. You can also use a pair of empty parentheses following the length of the array to initialize the value of the array element, such as pi2.

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.