Passing dereferenced new pointer to reference paramter

Discussion in 'C++' started by shanemh@gmail.com, Oct 28, 2006.

  1. Guest

    I'm starting out with c++ and for some reason I cant get my brain
    around this one:

    If I have the following:

    void Foo (someClass& x)
    {}

    int Main (void)
    {
    Foo(*(new someClass));
    }


    Is the memory allocated for the 'new someClass' that is passed to Foo,
    automatically deleted, or must it be deleted using 'delete'? If so, how
    given that the new someClass is never actually assigned to a pointer?
    Or, is it assigned to x... i'm not sure....i think i might be missing
    something here in the way c++ treats this kind of thing.

    Thanks
    , Oct 28, 2006
    #1
    1. Advertising

  2. * :
    > I'm starting out with c++ and for some reason I cant get my brain
    > around this one:
    >
    > If I have the following:
    >
    > void Foo (someClass& x)
    > {}
    >
    > int Main (void)


    Note 1: C++ is a case-sensitive language; that should be 'main'.
    Note 2: C++ is not C, using 'void' there is a C'ism best a-voided.
    Note 3: When posting code, copy and paste code that compiles.

    > {
    > Foo(*(new someClass));
    > }
    >
    >
    > Is the memory allocated for the 'new someClass' that is passed to Foo,
    > automatically deleted


    Not during program execution.


    >, or must it be deleted using 'delete'? If so, how
    > given that the new someClass is never actually assigned to a pointer?


    Do this:

    int main()
    {
    someClass o;
    Foo( o );
    }

    Forget about pointers and 'new' until you have mastered local objects
    and standard library container classes.


    > Or, is it assigned to x... i'm not sure....i think i might be missing
    > something here in the way c++ treats this kind of thing.


    You do, yes (see above).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 28, 2006
    #2
    1. Advertising

  3. Guest

    Alf P. Steinbach wrote:
    > * :
    > > I'm starting out with c++ and for some reason I cant get my brain
    > > around this one:
    > >
    > > If I have the following:
    > >
    > > void Foo (someClass& x)
    > > {}
    > >
    > > int Main (void)

    >
    > Note 1: C++ is a case-sensitive language; that should be 'main'.
    > Note 2: C++ is not C, using 'void' there is a C'ism best a-voided.
    > Note 3: When posting code, copy and paste code that compiles.


    Oops. yeah i'll b more careful next time, notes noted.


    >
    > > {
    > > Foo(*(new someClass));
    > > }
    > >
    > >
    > > Is the memory allocated for the 'new someClass' that is passed to Foo,
    > > automatically deleted

    >
    > Not during program execution.



    So, you're saying it will be deleted after program execution?

    My understanding is that 'new' will return a pointer to a new instance
    of someClass. The
    pointer is then dereferenced and the someClass instance is passed by
    reference to Foo. So, x then references this instance of someClass.
    When Foo returns, x is deallocated, but the instance of someClass is
    not.

    The confusing part is that from what i've read, anything created with
    'new' must be deallocated with 'delete'. But how can the instance of
    someClass be deleted when it was never assigned to a 'pointer' that can
    be deleted?



    >
    >
    > >, or must it be deleted using 'delete'? If so, how
    > > given that the new someClass is never actually assigned to a pointer?

    >
    > Do this:
    >
    > int main()
    > {
    > someClass o;
    > Foo( o );
    > }
    >
    > Forget about pointers and 'new' until you have mastered local objects
    > and standard library container classes.


    yeah i agree, but this was just one of those problems that confused me
    when i saw it and my curiosity won. Also, i'd never actually code like
    this, it's just a curly one i want to understand.

    >
    >
    > > Or, is it assigned to x... i'm not sure....i think i might be missing
    > > something here in the way c++ treats this kind of thing.

    >
    > You do, yes (see above).
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
    , Oct 28, 2006
    #3
  4. Bo Persson Guest

    wrote:
    > Alf P. Steinbach wrote:
    >> * :
    >>> I'm starting out with c++ and for some reason I cant get my brain
    >>> around this one:
    >>>
    >>> If I have the following:
    >>>
    >>> void Foo (someClass& x)
    >>> {}
    >>>
    >>> int Main (void)

    >>
    >> Note 1: C++ is a case-sensitive language; that should be 'main'.
    >> Note 2: C++ is not C, using 'void' there is a C'ism best a-voided.
    >> Note 3: When posting code, copy and paste code that compiles.

    >
    > Oops. yeah i'll b more careful next time, notes noted.
    >
    >
    >>
    >>> {
    >>> Foo(*(new someClass));
    >>> }
    >>>
    >>>
    >>> Is the memory allocated for the 'new someClass' that is passed to
    >>> Foo, automatically deleted

    >>
    >> Not during program execution.

    >
    >
    > So, you're saying it will be deleted after program execution?


    No, but the OS will recover all the memory you program has been
    allocating.

    >
    > My understanding is that 'new' will return a pointer to a new
    > instance
    > of someClass. The
    > pointer is then dereferenced and the someClass instance is passed by
    > reference to Foo. So, x then references this instance of someClass.
    > When Foo returns, x is deallocated, but the instance of someClass is
    > not.


    Correct.

    >
    > The confusing part is that from what i've read, anything created
    > with
    > 'new' must be deallocated with 'delete'.


    Yes.

    > But how can the instance of
    > someClass be deleted when it was never assigned to a 'pointer' that
    > can be deleted?


    That's the problem with the code. :)

    In this case, the function Foo just might end with

    delete &x; // NOT recommended

    to recover the pointer, and delete the memory it points to. If you
    pass Foo a reference to something that is not dynamically allocated,
    this will absolutely not work. "Don't try this at home!".



    Bo Persson
    Bo Persson, Oct 28, 2006
    #4
  5. Shane posted:

    > Is the memory allocated for the 'new someClass' that is passed to Foo,
    > automatically deleted, or must it be deleted using 'delete'? If so, how
    > given that the new someClass is never actually assigned to a pointer?



    "new" is an operator. It takes an operand and evaluates to a value. Take
    the "unary negation" operator for example; it takes a number as a operand
    and evaluates to a value, e.g.:

    -5

    The operand is "5" and the entire expression evaluates to minus five.

    The kind of operand that "new" wants is a type. It evaluates to a memory
    address (i.e. the memory address of the allocated memory), e.g.:

    new int

    Dynamically allocated memory (e.g. memory allocated via "new", or via
    "malloc") is under the programmer's sole control. The memory is only
    deallocated when the programmer passes the memory address to "delete".

    Take a look at the following code:

    int main()
    {
    new int;
    }

    The memory address which "new int" evaluates to is discarded -- it isn't
    saved or stored in any way. Therefore, we have no way to pass this memory
    address to "delete", so we can't deallocate the memory. It's true that some
    systems deallocate all of a program's lingering memory after the program
    exits, but that's outside the scope of this newsgroup.

    The kind of variable we use for storing a memory address is a pointer
    variable. Furthermore, we pick a particular kind of pointer variable
    depending on what kind of object resides at the memory address in question.
    For our own sample, we do the following:

    int main()
    {
    int *const p = new int;
    }

    Now we have stored the memory address, and can use it in the future to free
    the memory. The storing of the memory address in a pointer variable has no
    effect whatsoever on the workings of the "new" operator. All "new" does is
    create an object and evaluate to a memory address. Here's a few different
    ways of doing it:

    int main()
    {
    int *const p1 = new int;
    delete p1;

    int &r1 = *new int;
    delete &r1;


    delete new int;
    }

    --

    Frederick Gotham
    Frederick Gotham, Oct 28, 2006
    #5
  6. Guest

    Thanks for your help everyone. It makes sense now :)
    , Oct 28, 2006
    #6
  7. Jack Klein Guest

    On Sat, 28 Oct 2006 12:08:08 +0200, "Bo Persson" <> wrote in
    comp.lang.c++:

    > wrote:
    > > Alf P. Steinbach wrote:
    > >> * :
    > >>> I'm starting out with c++ and for some reason I cant get my brain
    > >>> around this one:
    > >>>
    > >>> If I have the following:
    > >>>
    > >>> void Foo (someClass& x)
    > >>> {}
    > >>>
    > >>> int Main (void)
    > >>
    > >> Note 1: C++ is a case-sensitive language; that should be 'main'.
    > >> Note 2: C++ is not C, using 'void' there is a C'ism best a-voided.
    > >> Note 3: When posting code, copy and paste code that compiles.

    > >
    > > Oops. yeah i'll b more careful next time, notes noted.
    > >
    > >
    > >>
    > >>> {
    > >>> Foo(*(new someClass));
    > >>> }
    > >>>
    > >>>
    > >>> Is the memory allocated for the 'new someClass' that is passed to
    > >>> Foo, automatically deleted
    > >>
    > >> Not during program execution.

    > >
    > >
    > > So, you're saying it will be deleted after program execution?

    >
    > No, but the OS will recover all the memory you program has been
    > allocating.


    Can you cite a reference from the C++ standard that guarantees this?
    What if the OP is not using "the OS", but some other OS? What is "the
    OS", anyway?

    Exactly what makes you think that the C++, or any other language
    standard, for that matter, can impose requirements on any operating
    system?

    Or perhaps you have tried this on every single operating system which
    can run C++ programs, so you can speak for all of them?

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Oct 30, 2006
    #7
  8. Bo Persson Guest

    Jack Klein wrote:
    > On Sat, 28 Oct 2006 12:08:08 +0200, "Bo Persson" <> wrote
    > in comp.lang.c++:
    >
    >> wrote:
    >>> Alf P. Steinbach wrote:
    >>>> * :
    >>>>
    >>>>> {
    >>>>> Foo(*(new someClass));
    >>>>> }
    >>>>>
    >>>>>
    >>>>> Is the memory allocated for the 'new someClass' that is passed
    >>>>> to Foo, automatically deleted
    >>>>
    >>>> Not during program execution.
    >>>
    >>>
    >>> So, you're saying it will be deleted after program execution?

    >>
    >> No, but the OS will recover all the memory you program has been
    >> allocating.

    >
    > Can you cite a reference from the C++ standard that guarantees this?


    The point was actually that the system will not 'delete' the object (meaning
    no destructor called) at the end of execution. That's guaranteed by the C++
    standard.

    > What if the OP is not using "the OS", but some other OS? What is
    > "the OS", anyway?


    "The OS" is any operating system run on the OP's machine.

    If you go really basic, like a computerized toaster for example, you have to
    define what "after program execution" means. If it means "pull the plug",
    the memory resources will surely be reclaimed at the next restart.

    >
    > Exactly what makes you think that the C++, or any other language
    > standard, for that matter, can impose requirements on any operating
    > system?


    It can, in the sense that if the operating system doesn't supply the
    resources needed, you cannot have a conforming C++ implementation on that
    system.

    >
    > Or perhaps you have tried this on every single operating system
    > which can run C++ programs, so you can speak for all of them?


    Yes.


    Bo Persson
    Bo Persson, Oct 30, 2006
    #8
    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. matt
    Replies:
    10
    Views:
    3,310
    Grant Wagner
    Sep 2, 2005
  2. B. Penn
    Replies:
    6
    Views:
    546
    Old Wolf
    Aug 9, 2004
  3. Replies:
    2
    Views:
    312
  4. A.Gallus
    Replies:
    0
    Views:
    262
    A.Gallus
    Oct 7, 2008
  5. Sarah Allen

    passing a method as a paramter

    Sarah Allen, Mar 2, 2009, in forum: Ruby
    Replies:
    12
    Views:
    171
    Sarah Allen
    Mar 2, 2009
Loading...

Share This Page