PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Does this code has problems?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Does this code has problems?

Réponse
 
LinkBack Outils de la discussion
Vieux 16/07/2008, 05h04   #1
Jianwei Sun
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Does this code has problems?

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.


  Réponse avec citation
Vieux 16/07/2008, 05h13   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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++.


> 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?
  Réponse avec citation
Vieux 16/07/2008, 06h18   #3
Rajib
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does this code has problems?

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.
  Réponse avec citation
Vieux 16/07/2008, 14h03   #4
J.W.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does this code has problems?


----- 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?


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h52.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10881 seconds with 12 queries