Oh, my mistake. You
do have a mistake in your
pop function.
if (size > 1)
should be
if (size > 0)
.
i am deleting the newNode coz it is not needed .... does not that cause memory leak.?
why should i not delete newNode while i have already given that address to t->next? |
OK, you know that
t->next and
newNode are both pointers, right? That means that they contain memory addresses to the place where the actual node you created is stored.
So when you set
t->next = newNode;
, you're telling
t->next to copy the address stored in
newNode. When you
delete newNode;
, you are de-allocating the memory that it was pointing to. But since
t->next is pointing towards the same memory, well... do you see the problem now?
EDIT: Why do i post so slooooow?
EDIT2: Attempt at amusing anecdotal analogy
Supervillain "programmer": new, go find me somebody *
new goes off to find someone*
Henchman "new": Boss, here's someone's address *hands over an envelope labeled
newNode*
Supervillain "programmer": Thanks
new *copies the address in
newNode into another envelope labeled
t*
Supervillain "programmer": delete, go kill the person at this address *hands over the envelope labeled
newNode*
Henchman "delete": OK boss. *heads off to kill someone*
Supervillain "programmer": shakedown, go shake down the person at this address for all his cash *hands over the envelope labeled
t*
Henchman "shakedown": OK boss. *heads off to shake someone down, but returns angrily*
Henchman "shakedown": Boss, I went where you said but that stupid
delete already killed the guy! How am I supposed to shake down a dead person?