简体   繁体   中英

difference between two syntax new( int[ size ] ) and new int[ size ]

this text runs without any warnings or errors

int* iPtr;
unsigned int size;
cin >> size;
iPtr = new int[size];

this one return warning but works fine why!!

warning: non-constant array new length must be specified without parentheses around the type-id [-Wvla] iPtr = new (int[ size ]) ;

int* iPtr;
unsigned int size;
cin >> size;
iPtr = new(int[size]);

This particular warning is issued because variable length arrays are not permitted in C++. The parentheses cause the compiler to see int[size] as a variable length array.

That is what the -Wvla corresponds to in the warning.

If you specify a constant value instead of user-specified value for size , you can use parentheses.

int main() {
    unsigned int* iPtr;
    constexpr unsigned int size = 10;
    iPtr = new (unsigned int[size]);
}

See Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM