behavior of new

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tino

    behavior of new

    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
  • John Harrison

    #2
    Re: behavior of new


    "Tino" <tino52@yahoo.c om> wrote in message
    news:f9d112e6.0 308010922.5e568 d5@posting.goog le.com...[color=blue]
    > 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[/color]
    wouldn't be...[color=blue]
    >
    > Regards,
    > Ryan[/color]

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

    john



    Comment

    • Reginald P. Smithford

      #3
      Re: behavior of new

      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" <v.Abazarov@att Abi.com> wrote in message news:<vil8mk4dk k8n26@corp.supe rnews.com>...[color=blue]
      > "Tino" <tino52@yahoo.c om> wrote...[color=green]
      > > 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[/color]
      > 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[/color]

      Comment

      • Jerry Coffin

        #4
        Re: behavior of new

        In article <f9d112e6.03080 10922.5e568d5@p osting.google.c om>, tino52
        @yahoo.com says...[color=blue]
        > 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...[/color]

        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.

        Comment

        Working...