J
James Kanze
Allocating array[] with *new is standard,
I need more explanation plz.you mean like this?
size_t sz;
int array[]= * new int [sz];
this looks odd to me !!!!!!!!!!so ,wont one have to delete the
array explicitly?
Of course. And that wouldn't be the syntax.
In context, the * is really just a typo---what I was thinking
about was:
int* pArray = new int[ size ] ;
Something like
int& rFirst = *new int[ size ] ;
// ...
delete [] &rFirst ;
is also legal, but not very useful, since the only way to access
beyond the first element is to take the address of rFirst, and
then do pointer arithmetic on it.
Something like:
int (&array)[ size ]
= reinterpret_cast< int(&)[ size ] >( *new int[ size ] ) ;
// ...
delete [] &array ;
is also legal, if size is a constant, but frankly, I wouldn't
recommend it.
When all is said and done, of course, if you are allocating
arrays dynamically, and their lifetime corresponds to some sort
of scope, there is absolutely no reason not to use std::vector.
If the lifetime is truely dynamic, then you can usually use
std::vector as well, but there will be cases where it will
introduce an additional indirection.
I used to think that new was used for pointer to arrays.
You mean that new of an array returns a pointer to the first
element, and not a pointer to the array. That's true (except
that the first element is guaranteed to have the same address as
the array, so my reinterpret_cast above works).