469,641 Members | 1,129 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

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.
Jul 16 '08 #1
1 971
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.
Jul 16 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

37 posts views Thread by Curt | last post: by
43 posts views Thread by Minti | last post: by
13 posts views Thread by Jason Huang | last post: by
14 posts views Thread by Anoop | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.