|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
* 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++. > 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. 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? |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
----- Original Message ----- From: "Alf P. Steinbach" <alfps@start.no> 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 s 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? |
|
![]() |
| Outils de la discussion | |
|
|