Is there any difference between new char[n]
and new (char[n])
?
I have the second case in a generated code, g++ (4.8.0) gives me
ISO C++ does not support variable-length array types [-Wvla]
This makes me think if these two are the same or not.
new char[n]
means "allocaten
objects of typechar
.- does
new (char[n])
mean "allocate 1 object of typearray of n chars
"? - deleting the first is clear.
- should I delete the second with
delete
ordelete[]
? - are there any other differences I should be aware of?
- may I safely remove the parentheses and turn the second case into the first, when other parts of the software expect the second?
The code is generated by a third party software (and used by other parts of the software), so I cannot just "use vector instead".
This is minimal example:
int main (void)
{
int n(10);
int *arr = new (int[n]); // removing parentheses fixes warning
*arr = 0; // no "unused variable" warning
return 0;
}
int(*)[n]
toint*
.int*
. Theyint(*)[n]
can't exist as a type.