Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > new & delete

Reply
Thread Tools

new & delete

 
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      07-24-2007

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.

 
Reply With Quote
 
 
 
 
Junhui Tong
Guest
Posts: n/a
 
      07-24-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-24-2007
Junhui Tong wrote:
> (E-Mail Removed) wrote:
>> 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.


.... 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      07-25-2007
On 24 Jul, 15:35, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Junhui Tong wrote:
> > (E-Mail Removed) wrote:
> >> 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.

>
> ... 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?


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-25-2007
(E-Mail Removed) wrote:
> On 24 Jul, 15:35, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> Junhui Tong wrote:
>>> (E-Mail Removed) wrote:
>>>> 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.

>>
>> ... 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?


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      07-25-2007
On 25 Jul, 13:26, "Victor Bazarov" <(E-Mail Removed)> wrote:

> 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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-25-2007
(E-Mail Removed) wrote:
> On 25 Jul, 13:26, "Victor Bazarov" <(E-Mail Removed)> wrote:
>
>> 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/wg...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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"return delete (new int)" compile but "return delete (new X X C++ 4 07-19-2010 05:47 PM
Mixing new/delete and operator new/delete? Jef Driesen C++ 1 01-19-2005 01:56 PM
Can someone tell me why I can't delete this file? and why it blue screens WinXP Pro on delete? zZz Computer Support 1 01-12-2005 02:37 AM
Cannot Delete, (The Delete Key Won't Work) Lee Something Computer Support 13 10-15-2003 09:51 PM
Overloading new[] and delete[]: how do they vary from new and delete? HeroOfSpielburg C++ 1 08-06-2003 03:58 AM



Advertisments