Question about this piece of code.

Discussion in 'C++' started by Jianwei Sun, Aug 10, 2008.

  1. Jianwei Sun

    Jianwei Sun Guest

    I am reading a peice of code which is at the following link:
    http://www.brpreiss.com/books/opus4/html/page141.html#SECTION007123000000000000000

    The code is like this:

    Object& StackAsLinkedList::pop()
    {
    if(count==0)
    throw domain_error("stack is empty");

    Object& const result=*list.First();
    list.Extract(&result);
    --count;
    return result;
    }

    Does this code return a reference to local variable result? If this is the
    case, then this code
    has problem?

    Thanks,
    J.W.
     
    Jianwei Sun, Aug 10, 2008
    #1
    1. Advertising

  2. Jianwei Sun

    Jianwei Sun Guest


    > I am reading a peice of code which is at the following link:
    > http://www.brpreiss.com/books/opus4/html/page141.html#SECTION007123000
    > 000000000000
    >
    > The code is like this:
    >
    > Object& StackAsLinkedList::pop()
    > {
    > if(count==0)
    > throw domain_error("stack is empty");
    > Object& const result=*list.First();
    > list.Extract(&result);
    > --count;
    > return result;
    > }
    > Does this code return a reference to local variable result? If this is
    > the case, then this code has problem?
    >
    > Thanks,
    > J.W.


    The list extract function is here:

    http://www.brpreiss.com/books/opus4/html/page98.html#SECTION0052100000000000000000
     
    Jianwei Sun, Aug 10, 2008
    #2
    1. Advertising

  3. Jianwei Sun

    Salt_Peter Guest

    On Aug 10, 11:12 am, Jianwei Sun <> wrote:
    > I am reading a peice of code which is at the following link:http://www.brpreiss.com/books/opus4/html/page141.html#SECTION00712300...
    >
    > The code is like this:
    >
    > Object& StackAsLinkedList::pop()
    > {
    > if(count==0)
    > throw domain_error("stack is empty");
    >
    > Object& const result=*list.First();
    > list.Extract(&result);
    > --count;
    > return result;
    >
    > }
    >
    > Does this code return a reference to local variable result? If this is the
    > case, then this code
    > has problem?
    >


    the function returns a reference (as required since the function's
    return type is Object&).
    The declaration for result is a const reference:
    Object& const result(*list.First());
    In effect, you aren't dealing with a temporary variable. Passing
    references by value doesn't magicly allocate or deallocate its
    referant.

    Whether the code is correct is unknown to us. In particular, no-one
    here knows what happens to result (the Object) during and after the
    call to list.Extract(&result). We have no way of knowing how (or if)
    the list is managing the Object lifetimes.
     
    Salt_Peter, Aug 10, 2008
    #3
  4. Jianwei Sun

    Jianwei Sun Guest

    Hello Salt_Peter,

    > On Aug 10, 11:12 am, Jianwei Sun <> wrote:
    >
    >> I am reading a peice of code which is at the following
    >> link:http://www.brpreiss.com/books/opus4/html/page141.html#SECTION007
    >> 12300...
    >>
    >> The code is like this:
    >>
    >> Object& StackAsLinkedList::pop()
    >> {
    >> if(count==0)
    >> throw domain_error("stack is empty");
    >> Object& const result=*list.First();
    >> list.Extract(&result);
    >> --count;
    >> return result;
    >> }
    >>
    >> Does this code return a reference to local variable result? If this
    >> is the
    >> case, then this code
    >> has problem?

    > the function returns a reference (as required since the function's
    > return type is Object&).
    > The declaration for result is a const reference:
    > Object& const result(*list.First());
    > In effect, you aren't dealing with a temporary variable. Passing
    > references by value doesn't magicly allocate or deallocate its
    > referant.
    > Whether the code is correct is unknown to us. In particular, no-one
    > here knows what happens to result (the Object) during and after the
    > call to list.Extract(&result). We have no way of knowing how (or if)
    > the list is managing the Object lifetimes.
    >


    Hi, Peter,

    The extact function is here:

    http://www.brpreiss.com/books/opus4/html/page98.html#SECTION0052100000000000000000

    If I get your meaning correclty, if the extract is deleting this node, then
    this won't be correct.
    If the extact doesn't delete this node, this will work fine.

    Thanks,
    J.W.
     
    Jianwei Sun, Aug 10, 2008
    #4
  5. Jianwei Sun

    Guest

    On Aug 10, 11:18 am, Jianwei Sun <> wrote:
    > > I am reading a peice of code which is at the following link:
    > >http://www.brpreiss.com/books/opus4/html/page141.html#SECTION007123000
    > > 000000000000

    >
    > > The code is like this:

    >
    > > Object& StackAsLinkedList::pop()
    > > {
    > > if(count==0)
    > > throw domain_error("stack is empty");
    > > Object& const result=*list.First();
    > > list.Extract(&result);
    > > --count;
    > > return result;
    > > }
    > > Does this code return a reference to local variable result? If this is
    > > the case, then this code has problem?

    >
    > > Thanks,
    > > J.W.

    >
    > The list extract function is here:
    >
    > http://www.brpreiss.com/books/opus4/html/page98.html#SECTION005210000...- Hide quoted text -
    >
    > - Show quoted text -

    If we ignore that result is a const and the return type is non-const,
    it would seem that the returned value (a reference) refers to an
    object that has been deleted inside the Extract function. This, of
    course, is wrong.
     
    , Aug 10, 2008
    #5
  6. Jianwei Sun

    Jianwei Sun Guest

    Hello ,

    > On Aug 10, 11:18 am, Jianwei Sun <> wrote:
    >
    >>> I am reading a peice of code which is at the following link:
    >>> http://www.brpreiss.com/books/opus4/html/page141.html#SECTION0071230
    >>> 00 000000000000
    >>>
    >>> The code is like this:
    >>>
    >>> Object& StackAsLinkedList::pop()
    >>> {
    >>> if(count==0)
    >>> throw domain_error("stack is empty");
    >>> Object& const result=*list.First();
    >>> list.Extract(&result);
    >>> --count;
    >>> return result;
    >>> }
    >>> Does this code return a reference to local variable result? If this
    >>> is
    >>> the case, then this code has problem?
    >>> Thanks,
    >>> J.W.

    >> The list extract function is here:
    >>
    >> http://www.brpreiss.com/books/opus4/html/page98.html#SECTION005210000
    >> ...- Hide quoted text -
    >>
    >> - Show quoted text -
    >>

    > If we ignore that result is a const and the return type is non-const,
    > it would seem that the returned value (a reference) refers to an
    > object that has been deleted inside the Extract function. This, of
    > course, is wrong.
    >


    Thank you for verify, I just want to make sure I didn't miss something obvious
    since the author who maintains
    this tutorial is a very knowledgeable person and this book is a 5-star book
    on amazon.com

    Thanks,
    J.W.
     
    Jianwei Sun, Aug 10, 2008
    #6
  7. Jianwei Sun

    Triple-DES Guest

    On 10 Aug, 19:56, Jianwei Sun <> wrote:
    > Thank you for verify, I just want to make sure I didn't miss something obvious
    > since the author who maintains
    > this tutorial is a very knowledgeable person and this book is a 5-star book
    > on amazon.com
    >
    > Thanks,
    > J.W


    Still, I think the author's approach is inherently flawed. Why would
    you want to implement your own data structures based on storing
    pointers to an Object base class in C++? That's more like pre-generics
    Java than idiomatic C++.

    For C++, I recommend reading up on the STL instead.

    DP
     
    Triple-DES, Aug 11, 2008
    #7
    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. Przemo
    Replies:
    5
    Views:
    545
    Teemu Keiski
    Jul 18, 2004
  2. =?Utf-8?B?RGlmZmlkZW50?=

    Question on optimizing a piece of code

    =?Utf-8?B?RGlmZmlkZW50?=, Jun 9, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    410
    Patrice
    Jun 9, 2005
  3. Patrick Plattes

    Download a file piece by piece

    Patrick Plattes, Nov 30, 2006, in forum: Ruby
    Replies:
    2
    Views:
    237
    Patrick Plattes
    Nov 30, 2006
  4. Gaba Luschi

    question about a small piece of code

    Gaba Luschi, Feb 20, 2011, in forum: Ruby
    Replies:
    2
    Views:
    117
    masdel
    Feb 20, 2011
  5. Fendi Baba
    Replies:
    2
    Views:
    108
    Effendi Baba
    Jul 9, 2003
Loading...

Share This Page