Questions regarding the C++ standard

Discussion in 'C++' started by aegis, Sep 7, 2009.

  1. aegis

    aegis Guest

    If I have a reference type such as:
    T& obj;

    and assuming it is initialized to an object
    of type T, then the object type of the following
    expression:
    &obj;

    would be "Pointer to T" or "Pointer to T&"?

    Also, if I dynamically allocate an array of
    pointers to T, as follows:
    T **p = new T*[n] for some integer n
    and further populate it with pointer
    values to T objects, then does:
    delete [] p
    handle the freeing of memory for all T objects
    and then the space for the array of T* objects?
     
    aegis, Sep 7, 2009
    #1
    1. Advertising

  2. * aegis:
    > If I have a reference type such as:
    > T& obj;
    >
    > and assuming it is initialized to an object
    > of type T, then the object type of the following
    > expression:
    > &obj;
    >
    > would be "Pointer to T" or "Pointer to T&"?


    Except that macros don't respect scopes and such things you can think of

    T& obj = otherObject;

    as being translated to or equivalent to

    T* const __pobj = &otherObject;
    #define obj (*__pobj)

    Thus when you do &obj it's the same as

    &(*__pobj)

    which is effectively the same as

    &(*&otherObject)

    And I guess you can figure it out from that.

    It provides a general platform for figuring out the answer to (almost) any
    question you have about references.


    > Also, if I dynamically allocate an array of
    > pointers to T, as follows:
    > T **p = new T*[n] for some integer n
    > and further populate it with pointer
    > values to T objects, then does:
    > delete [] p
    > handle the freeing of memory for all T objects
    > and then the space for the array of T* objects?


    No.


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Sep 7, 2009
    #2
    1. Advertising

  3. aegis

    aegis Guest

    On Sep 7, 10:42 am, "Alf P. Steinbach" <> wrote:
    > * aegis:
    >
    > > If I have a reference type such as:
    > > T& obj;

    >
    > > and assuming it is initialized to an object
    > > of type T, then the object type of the following
    > > expression:
    > >  &obj;

    >
    > > would be "Pointer to T" or "Pointer to T&"?

    >
    > Except that macros don't respect scopes and such things you can think of
    >
    >    T& obj = otherObject;
    >
    > as being translated to or equivalent to
    >
    >    T* const __pobj = &otherObject;
    >    #define obj (*__pobj)
    >
    > Thus when you do &obj it's the same as
    >
    >    &(*__pobj)
    >
    > which is effectively the same as
    >
    >    &(*&otherObject)
    >
    > And I guess you can figure it out from that.
    >
    > It provides a general platform for figuring out the answer to (almost) any
    > question you have about references.
    >


    Thanks.

    > > Also, if I dynamically allocate an array of
    > > pointers to T, as follows:
    > > T **p = new T*[n] for some integer n
    > > and further populate it with pointer
    > > values to T objects, then does:
    > > delete [] p
    > > handle the freeing of memory for all T objects
    > > and then the space for the array of T* objects?

    >
    > No.
    >


    So it just frees the allocated space for the array
    of pointer to T objects?
     
    aegis, Sep 7, 2009
    #3
  4. aegis

    SG Guest

    On 7 Sep., 19:13, aegis wrote:

    > > > Also, if I dynamically allocate an array of
    > > > pointers to T, as follows:
    > > > T **p = new T*[n] for some integer n
    > > > and further populate it with pointer
    > > > values to T objects, then does:
    > > > delete [] p
    > > > handle the freeing of memory for all T objects
    > > > and then the space for the array of T* objects?

    >
    > > No.

    >
    > So it just frees the allocated space for the array
    > of pointer to T objects?


    Yes. The elements of this array are correctly destroyed and the memory
    of this array is freed. It's just that if you destroy a pointer it
    doesn't magically free the memory it points to. A raw pointer doesn't
    have a destructor that does so.
     
    SG, Sep 7, 2009
    #4
  5. aegis wrote:
    > would be "Pointer to T" or "Pointer to T&"?


    There's no such a thing in C++ as "pointer to T&". That should answer
    your question.

    > Also, if I dynamically allocate an array of
    > pointers to T, as follows:
    > T **p = new T*[n] for some integer n
    > and further populate it with pointer
    > values to T objects, then does:
    > delete [] p
    > handle the freeing of memory for all T objects
    > and then the space for the array of T* objects?


    Is there a delete for each new you did?
     
    Juha Nieminen, Sep 7, 2009
    #5
    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. steve.leach

    How standard is the standard library?

    steve.leach, Apr 18, 2005, in forum: Python
    Replies:
    1
    Views:
    395
    Christos TZOTZIOY Georgiou
    Apr 18, 2005
  2. Ali Syed
    Replies:
    3
    Views:
    562
    Mark McIntyre
    Oct 13, 2004
  3. arnuld
    Replies:
    3
    Views:
    414
    arnuld
    Jul 24, 2006
  4. Shao Miller

    C Standard Regarding Null Pointer Dereferencing

    Shao Miller, Jul 21, 2010, in forum: C Programming
    Replies:
    280
    Views:
    4,364
  5. Älphä Blüë

    Standard Deviation question regarding EDOM errors

    Älphä Blüë, Jul 21, 2009, in forum: Ruby
    Replies:
    4
    Views:
    121
    Älphä Blüë
    Jul 22, 2009
Loading...

Share This Page