behavior of new

Discussion in 'C++' started by Tino, Aug 1, 2003.

  1. Tino

    Tino Guest

    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
     
    Tino, Aug 1, 2003
    #1
    1. Advertising

  2. "Tino" <> wrote in message
    news:...
    > 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
     
    John Harrison, Aug 1, 2003
    #2
    1. Advertising

  3. 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" <> wrote in message news:<>...
    > "Tino" <> wrote...
    > > 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
     
    Reginald P. Smithford, Aug 4, 2003
    #3
  4. Tino

    Jerry Coffin Guest

    In article <>, tino52
    @yahoo.com says...
    > 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.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Aug 6, 2003
    #4
    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. Sorin Dolha [MCSD .NET]
    Replies:
    9
    Views:
    2,962
    Sorin Dolha [MCSD .NET]
    Mar 2, 2004
  2. Ryan Mack
    Replies:
    0
    Views:
    370
    Ryan Mack
    May 24, 2004
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,805
    Dan Pop
    Feb 17, 2004
  4. Larry Hastings
    Replies:
    0
    Views:
    271
    Larry Hastings
    Jul 20, 2006
  5. Replies:
    2
    Views:
    487
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page