new & delete

K

keith

Could someone with access to the standard please tell me what it says
(if anything) about new initialising the allocated memory to zero, and/
or delete setting the pointer to NULL? Thx.
 
J

Junhui Tong

Could someone with access to the standard please tell me what it says
(if anything) about new initialising the allocated memory to zero, and/
or delete setting the pointer to NULL? Thx.
The short answer is
new will *not* initialize the allocated memory to zero, and
delete will *not* set the pointer to NULL.
 
V

Victor Bazarov

Junhui said:
The short answer is
new will *not* initialize the allocated memory to zero, and
delete will *not* set the pointer to NULL.

.... because the Standard does *not* say they shall.

There is, however, different ways to initialise the object/array one
obtains from 'new'. Empty parentheses, for example, provide value-
intialisation, which for built-in types means zero-initialisation.
For example

bool *pTF = new bool[50]();

allocates an array of fifty bool objects and initialises them all to
'false', then returns the pointer to the first object and initialises
'pTF' with it.

V
 
K

keith

... because the Standard does *not* say they shall.

There is, however, different ways to initialise the object/array one
obtains from 'new'. Empty parentheses, for example, provide value-
intialisation, which for built-in types means zero-initialisation.

Nicely clarified - thanks. Does the standard explicitly say that new
*shall not* initialise memory, or is it implementation dependant?
 
V

Victor Bazarov

Nicely clarified - thanks. Does the standard explicitly say that new
*shall not* initialise memory, or is it implementation dependant?

New will always "initialise" memory. However, the result of that
"initialisation" depends on the form of 'new' and the type you allocate.
For example, if you omit the initialiser altogether, objects will be
"default-initialised" which for non-POD means invoking their default
constructor and for POD types means leaving them uninitialised, etc.

It's all specified in [expr.new]/15 (5.3.4/15).

V
 
K

keith

New will always "initialise" memory. However, the result of that
"initialisation" depends on the form of 'new' and the type you allocate.
For example, if you omit the initialiser altogether, objects will be
"default-initialised" which for non-POD means invoking their default
constructor and for POD types means leaving them uninitialised, etc.

I think this is where I have been having difficulty. If, for example
I have a structure/class something like this:

class XYZPrivate
{
public:
unsigned char ucArray[1024];
unsigned int uiValue;
};

And then somewhere I do this:

XYZPrivate* p_ = new XYZPrivate();

will this initialise both the array and the value to zero, or just the
value? I could of course put an explicit constructor in the class to
memset stuff to zero, but that just seems inelegant.
It's all specified in [expr.new]/15 (5.3.4/15).

Sadly I don't have access to the standard at present. I appreciate
your assistance.
 
V

Victor Bazarov

New will always "initialise" memory. However, the result of that
"initialisation" depends on the form of 'new' and the type you
allocate. For example, if you omit the initialiser altogether,
objects will be "default-initialised" which for non-POD means
invoking their default constructor and for POD types means leaving
them uninitialised, etc.

I think this is where I have been having difficulty. If, for example
I have a structure/class something like this:

class XYZPrivate
{
public:
unsigned char ucArray[1024];
unsigned int uiValue;
};

And then somewhere I do this:

XYZPrivate* p_ = new XYZPrivate();

will this initialise both the array and the value to zero, or just the
value? I could of course put an explicit constructor in the class to
memset stuff to zero, but that just seems inelegant.

It should value-initialise all members, i.e. make the ucArray elements
and the uiValue all _zero_.
It's all specified in [expr.new]/15 (5.3.4/15).

Sadly I don't have access to the standard at present.

Download the next draft (document n2315) from
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2315.pdf

That should get you started. When you have spare $18 and a credit card,
spend it on a copy of the real thing from ANSI store.
I appreciate
your assistance.

You're welcome!

V
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top