int *iPtr = new int;

Discussion in 'C++' started by versesane, Jun 23, 2008.

  1. versesane

    versesane Guest

    So I was asked what happens when new is called. I said it acquires
    memory -> calls the constructor -> does the typecasting and returns
    the pointer. If there is no memory available it throws a bad_alloc. So
    this guy asked me

    int *iPtr = new int;

    What happens in the above case ?

    I think int is a primitive data type. It wouldn't hv any constructor.
    What happens here ? What is new's role here.

    Regards
    Ankur
     
    versesane, Jun 23, 2008
    #1
    1. Advertising

  2. versesane

    Kai-Uwe Bux Guest

    versesane wrote:

    > So I was asked what happens when new is called. I said it acquires
    > memory -> calls the constructor -> does the typecasting and returns
    > the pointer. If there is no memory available it throws a bad_alloc. So
    > this guy asked me
    >
    > int *iPtr = new int;
    >
    > What happens in the above case ?
    >
    > I think int is a primitive data type. It wouldn't hv any constructor.
    > What happens here ? What is new's role here.


    You get a pointer with an indeterminate pointee value [5.3.4/15]:

    A new-expression that creates an object of type T initializes that object
    as follows:
    ? If the new-initializer is omitted:
    - If T is a (possibly cv-qualified) non-POD class type [...]
    ? Otherwise, the object created has indeterminate value. [...]
    [...]


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 23, 2008
    #2
    1. Advertising

  3. versesane wrote:
    > I think int is a primitive data type. It wouldn't hv any constructor.
    > What happens here ? What is new's role here.


    Actually it has a constructor, but if you want to invoke it, you have
    to do it more explicitly:

    int* ptr = new int();

    Without the parentheses the value is not initialized.
     
    Juha Nieminen, Jun 23, 2008
    #3
  4. Juha Nieminen wrote:
    > versesane wrote:
    >> I think int is a primitive data type. It wouldn't hv any constructor.
    >> What happens here ? What is new's role here.

    >
    > Actually it has a constructor, but if you want to invoke it, you have
    > to do it more explicitly:
    >
    > int* ptr = new int();


    No, it doesn't have a constructor. When you create it with 'new int()',
    the '()' initializer triggers _initialization_, which sets the new
    object to 0. No constructor is involved here.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jun 23, 2008
    #4
  5. Andrey Tarasevich wrote:
    > No, it doesn't have a constructor. When you create it with 'new int()',
    > the '()' initializer triggers _initialization_, which sets the new
    > object to 0. No constructor is involved here.


    The relevant difference being?

    It looks like a constructor call, it acts like a constructor call, and
    it initializes the created object, as a constructor does. For all
    relevant purposes it is a constructor call. You can ever give it a
    parameter, like you can do to a constructor (like "new int(5)").

    What the machine code generated by the compiler ends up looking like
    is completely irrelevant.
     
    Juha Nieminen, Jun 24, 2008
    #5
  6. Juha Nieminen wrote:
    > Andrey Tarasevich wrote:
    >> No, it doesn't have a constructor. When you create it with 'new int()',
    >> the '()' initializer triggers _initialization_, which sets the new
    >> object to 0. No constructor is involved here.

    >
    > The relevant difference being?
    >
    > It looks like a constructor call,


    No, you got it backwards. It doesn't look like a "constructor call". C++
    language has no syntax for "constructor calls". The '()' part (possibly
    with arguments) is called "initializer" in C++. And the process it
    triggers is called "initialization". "Construction" (and constructor
    calls) is just one very specific and very particular form of
    "initialization", specific to class types only. "Initialization" in C++
    is significantly wider concept than "construction".

    > it acts like a constructor call,


    The semantics of initialization is described in C++ standard. And no, it
    doesn't act as a constructor call.

    > and
    > it initializes the created object, as a constructor does.


    Just because constructors initialize objects doesn't mean that every
    initialization somehow involves constructor. Once again,
    "initialization" in C++ is significantly wider concept than "construction".

    > For all
    > relevant purposes it is a constructor call.


    No, it isn't. Otherwise, for example, one logical conclusion that would
    follow from this would be to expect that the very same default
    constructor should be called for automatic variables of type 'int'
    declared without an initializer. As we all know, it is not the case.

    > You can ever give it a
    > parameter, like you can do to a constructor (like "new int(5)").


    There are lots of things in C++ that can take a parameter. That doesn't
    make them into "constructors" though.

    > What the machine code generated by the compiler ends up looking like
    > is completely irrelevant.


    Machine code is not involved here in any way. What I'm saying is true on
    the conceptual level of C++ language.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jun 24, 2008
    #6
    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. Schnoffos
    Replies:
    2
    Views:
    1,220
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  3. Replies:
    6
    Views:
    437
    Andrey Tarasevich
    Nov 16, 2005
  4. Replies:
    6
    Views:
    309
  5. Replies:
    9
    Views:
    436
    James Kanze
    Apr 17, 2007
Loading...

Share This Page