new & delete

Discussion in 'C++' started by keith@bytebrothers.co.uk, Jul 24, 2007.

  1. Guest

    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.
     
    , Jul 24, 2007
    #1
    1. Advertising

  2. Junhui Tong Guest

    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.
     
    Junhui Tong, Jul 24, 2007
    #2
    1. Advertising

  3. Junhui Tong wrote:
    > 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
     
    Victor Bazarov, Jul 24, 2007
    #3
  4. Guest

    On 24 Jul, 15:35, "Victor Bazarov" <> wrote:
    > Junhui Tong wrote:
    > > 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?
     
    , Jul 25, 2007
    #4
  5. wrote:
    > On 24 Jul, 15:35, "Victor Bazarov" <> wrote:
    >> Junhui Tong wrote:
    >>> 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
     
    Victor Bazarov, Jul 25, 2007
    #5
  6. Guest

    On 25 Jul, 13:26, "Victor Bazarov" <> 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.
     
    , Jul 25, 2007
    #6
  7. wrote:
    > On 25 Jul, 13:26, "Victor Bazarov" <> 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/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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 25, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HeroOfSpielburg
    Replies:
    1
    Views:
    393
    Alf P. Steinbach
    Aug 6, 2003
  2. Jef Driesen
    Replies:
    1
    Views:
    505
    Gernot Frisch
    Jan 19, 2005
  3. [rob desbois]
    Replies:
    2
    Views:
    284
    [rob desbois]
    Apr 15, 2008
  4. X X
    Replies:
    4
    Views:
    358
    red floyd
    Jul 19, 2010
  5. Replies:
    2
    Views:
    470
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page