Two ways to initialize a dynamic array with new in C + + __c++

Source: Internet
Author: User

This new feature has been introduced in c++11, that is, it can be initialized directly when a dynamic array is requested in the form of: int* p = new int[cnt] (), where the CNT length and int types can be defined by themselves.

The problem I found was:

Use int* p = new INT[CNT] () in the VC compiler () and int* p = new int[cnt]{}; Either way, and you can use int* a = new int[10] {1,2,3,4,5,6,7,8,9,10}; Each member is assigned a value.

With MinGW, which is the g++ compiler, int* p = new int[cnt]{} is not supported; The error of the hint is "extended initializer lists only available with-std=c++0x ++0x ".

That is, {} is an extension of (), preferably unified (), to avoid the pit.

At the same time this initialization is a new feature of C11, you need to verify that your GCC compiler supports C11 features.


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.