Does this code has problems?

Discussion in 'C++' started by Jianwei Sun, Jul 16, 2008.

  1. Jianwei Sun

    Jianwei Sun Guest

    Hello, all,

    I am reading some code from an interview book.


    It has a stack pop function implementation like this..

    typedef struct Element {
    struct Element *next;
    void *data;
    } Element;



    bool pop( Element **stack, void **data ){
    Element *elem;
    if (!(elem = *stack)) return false;

    *data = elem->data;
    *stack = elem->next;
    delete elem;
    return true;
    }

    my question is this, since you already delete the element in the pop function,
    the data returned will be invalid, since it's part of the element.

    Can anybody points out whether I have some misunderstanding here?

    Thanks,
    J.W.
     
    Jianwei Sun, Jul 16, 2008
    #1
    1. Advertising

  2. Jianwei Sun

    Rajib Guest

    Jianwei Sun wrote:
    > Hello, all,
    >
    > I am reading some code from an interview book.
    >
    >
    > It has a stack pop function implementation like this..
    >
    > typedef struct Element {
    > struct Element *next;
    > void *data;
    > } Element;
    >
    >
    >
    > bool pop( Element **stack, void **data ){
    > Element *elem;
    > if (!(elem = *stack)) return false;
    >
    > *data = elem->data;
    > *stack = elem->next;
    > delete elem;
    > return true;
    > }
    >
    > my question is this, since you already delete the element in the pop
    > function,
    > the data returned will be invalid, since it's part of the element.
    >
    > Can anybody points out whether I have some misunderstanding here?
    >
    > Thanks,
    > J.W.
    >
    >

    pop doesn't delete data, it just elem, which just points to data.
     
    Rajib, Jul 16, 2008
    #2
    1. Advertising

  3. Jianwei Sun

    J.W. Guest

    ----- Original Message -----
    From: "Alf P. Steinbach" <>
    Newsgroups: comp.lang.c++
    Sent: Tuesday, July 15, 2008 11:13 PM
    Subject: Re: Does this code has problems?


    >* Jianwei Sun:
    >>
    >> I am reading some code from an interview book.

    >
    > What's that?
    >
    >
    >> It has a stack pop function implementation like this..
    >>
    >> typedef struct Element {
    >> struct Element *next;
    >> void *data;
    >> } Element;

    >
    > Except if this is code that's wrapped by a template class, and except if
    > this is code that's meant to illustrate Really Bad Coding, you should
    > throw that book on the flames (after appropriately informing this group
    > about which book it is).
    >
    > First, void* is an abomination, except for the mentioned wrapping by a
    > template.
    >
    > Second, the 'typedef' is C style, completely unnecessary in C++.
    >


    [ This is a pretty decent book, and it's my fault that I didn't mention the
    original code is C style, not c++ style].


    >
    >> bool pop( Element **stack, void **data ){

    >
    > In C++ those arguments should be references.
    >
    >
    >> Element *elem;
    >> if (!(elem = *stack)) return false;
    >>
    >> *data = elem->data;
    >> *stack = elem->next;
    >> delete elem;
    >> return true;
    >> }
    >>
    >> my question is this, since you already delete the element in the pop
    >> function,
    >> the data returned will be invalid, since it's part of the element.

    >
    > No, the data is not part of the element: the element just contains a
    > pointer to the data.
    >
    >


    [Thanks, this helps me a lot. Further question, as you said, the data is not
    part of the elment, so
    in the destructor, I need delete both the data and element since the element
    just contains a pointer to the data. ]

    > Cheers, & hth.,
    >
    > - Alf
    >
    >
    > PS: Now don't forget to (1) tell us which book it is, and (2) burn it.
    >
    > --
    > 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?
     
    J.W., Jul 16, 2008
    #3
    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. Prodip Saha
    Replies:
    2
    Views:
    448
    Prodip Saha
    Mar 2, 2005
  2. Rob Nicholson
    Replies:
    12
    Views:
    805
    Edwin Knoppert
    Dec 6, 2005
  3. Min-Koo Seo
    Replies:
    0
    Views:
    411
    Min-Koo Seo
    Jun 21, 2004
  4. tom_usenet
    Replies:
    0
    Views:
    537
    tom_usenet
    Jun 21, 2004
  5. TS
    Replies:
    3
    Views:
    354
    =?Utf-8?B?VFM=?=
    Oct 6, 2006
Loading...

Share This Page