const char* = new char[6]

F

Frederick Gotham

S S posted:
Even if you do not put brackets () and write
int *p = new int[3];
then also it does the default initialisation for all 3 members of
array, any significance of brackets?


The Standard provides no such guarantee (well I'm 99% that it doesn't) --
if you want each element to be default-initialised, you'll have to use the
empty parentheses.

Testing it will do you no good, as I know of at least one implementation
that default-initialises new'ed memory regardless of whether you provide
the empty parentheses.

Anyhow, even if the empty parentheses were redundant, I would still put
them in, just as how I write:

int main()
{
static MyPOD arr[5] = {};
}

instead of:

int main()
{
static MyPOD arr[5];
}

Indeed, the chain brackets are redundant because static data always gets
default initialised... but nonetheless, they express clear intent.
 
N

Noah Roberts

S said:
Frederick Gotham wrote:
int *p = new int[3]();

Even if you do not put brackets () and write
int *p = new int[3];
then also it does the default initialisation for all 3 members of
array, any significance of brackets?

You are mistaken. No initialization occurs if the parens are not
there. Not even default initialization.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top