Thursday, May 24, 2007

Who moved my cheese?

... er ... pointer ?

Here is another real-life example of buggy code that I found.

This piece of code aims to create a copy of a doubly linked list
// - localHead is the head of the list of blocks being copied
// - new_blk is the block next to the tail the list being copied
// - bodyTail is the tail of the current list,
// at the end of which the newly copied list is appended

listBlk *tmpBlk = localHead;
listBlk *nxtBlk = NULL;
listBlk *dupFirstBlk = NULL;

while (tmpBlk && (tmpBlk != new_block))
{
nxtBlk = tmpBlk->copy();
if (dupFirstBlk)
{
nxtBlk->back(tmpBlk->back());
nxtBlk->next(tmpBlk->next());
}
else
{
nxtBlk->setBackNext(bodyTail);
nxtBlk->next(new_block);
dupFirstBlk = nxtBlk;
}
tmpBlk = tmpBlk->next();
}
nxtBlk->next(new_block);


This has beeing lying around dormant for years .... (just because the list never had more than one blocks, owing to the way the code is structured)

And once again, what is the problem here ?

2 comments:

Pijush said...

I have visited this blog many times and I must say this is very impressive. I am a comp sc engg from one of the good engineering colleges, and have been coded with C/C++ in college. But unfortunately after that I have only worked in ABAP (Programming Languages in SAP). Now after so many years I am forgetting that too. So started a blog on ABAP . But really appreciate your innovative posts in this blog, and these tips and tricks will be helpful one day when you will start forgetting :-)

Sigma said...

@Pijush: Thanks for the compliment :-))
I hope it helps me when I start forgetting it. And it helps people not to make these same mistakes :-)