behavior of new

T

Tino

What is the standard behavior, if any, of the following.

int *p, n = 0;
p = new int[n];

Is p guaranteed not to be NULL, because it's not clear to me why it wouldn't be...

Regards,
Ryan
 
J

John Harrison

Tino said:
What is the standard behavior, if any, of the following.

int *p, n = 0;
p = new int[n];

Is p guaranteed not to be NULL, because it's not clear to me why it wouldn't be...

Regards,
Ryan

Its guaranteed not to be NULL, why should it be?

john
 
R

Reginald P. Smithford

Wouldn't p==NULL if there wasn't enough memory for new to succeed? I
haven't been paying much attention to the standard recently, but I
assume this is still true. But, other than this error condition, p is
guaranteed to be a valid address (which NULL is not), and different
from any other pointers allocated with new or malloc (not just
different from other ints allocated).

Reggy

Victor Bazarov said:
Tino said:
What is the standard behavior, if any, of the following.

int *p, n = 0;
p = new int[n];

Is p guaranteed not to be NULL, because it's not clear to me why it
wouldn't be...

Yes, it is guaranteed not to be NULL, and it's guaranteed to be
different from any other "int"'s address in the system.

Victor
 
J

Jerry Coffin

tino52 said:
What is the standard behavior, if any, of the following.

int *p, n = 0;
p = new int[n];

Is p guaranteed not to be NULL, because it's not clear to me why
it wouldn't be...

Assuming new didn't throw bad_alloc, p will be a unique, non-null
pointer, but dereferencing it will give undefined results.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top