Friday, May 25, 2007

How would you find out if one of the pointers in a linked list is corrupted or not?

This is a really good interview question. The reason is that linked lists are used in a wide variety of scenarios and being able to detect and correct pointer corruptions might be a very valuable tool. For example, data blocks associated with files in a file system are usually stored as linked lists. Each data block points to the next data block. A single corrupt pointer can cause the entire file to be lost!





Discover and fix bugs when they corrupt the linked list and not when effect becomes visible in some other part of the program. Perform frequent consistency checks (to see if the linked list is indeed holding the data that you inserted into it).

It is good programming practice to set the pointer value to NULL immediately after freeing the memory pointed at by the pointer. This will help in debugging, because it will tell you that the object was freed somewhere beforehand. Keep track of how many objects are pointing to a object using reference counts if required.

Use a good debugger to see how the datastructures are getting corrupted and trace down the problem. Debuggers like ddd on linux and memory profilers like Purify, Electric fence are good starting points. These tools should help you track down heap corruption issues easily.

Avoid global variables when traversing and manipulating linked lists. Imagine what would happen if a function which is only supposed to traverse a linked list using a global head pointer accidently sets the head pointer to NULL!.

Its a good idea to check the addNode() and the deleteNode() routines and test them for all types of scenarios. This should include tests for inserting/deleting nodes at the front/middle/end of the linked list, working with an empty linked list, running out of memory when using malloc() when allocating memory for new nodes, writing through NULL pointers, writing more data into the node fields then they can hold (resulting in corrupting the (probably adjacent) "prev" and "next" pointer fields), make sure bug fixes and enhancements to the linked list code are reviewed and well tested (a lot of bugs come from quick and dirty bug fixing), log and handle all possible errors (this will help you a lot while debugging), add multiple levels of logging so that you can dig through the logs. The list is endless...

Each node can have an extra field associated with it. This field indicates the number of nodes after this node in the linked list. This extra field needs to be kept up-to-date when we inserte or delete nodes in the linked list (It might become slightly complicated when insertion or deletion happens not at end, but anywhere in the linked list). Then, if for any node, p->field > 0 and p->next == NULL, it surely points to a pointer corruption.

You could also keep the count of the total number of nodes in a linked list and use it to check if the list is indeed having those many nodes or not.



The problem in detecting such pointer corruptions in C is that its only the programmer who knows that the pointer is corrupted. The program has no way of knowing that something is wrong. So the best way to fix these errors is check your logic and test your code to the maximum possible extent. I am not aware of ways in C to recover the lost nodes of a corrupted linked list. C does not track pointers so there is no good way to know if an arbitrary pointer has been corrupted or not. The platform may have a library service that checks if a pointer points to valid memory (for instance on Win32 there is a IsBadReadPtr, IsBadWritePtr API.) If you detect a cycle in the link list, it's definitely bad. If it's a doubly linked list you can verify, pNode->Next->Prev == pNode.


I have a hunch that interviewers who ask this question are probably hinting at something called Smart Pointers in C++. Smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners. This topic is out of scope here, but you can find lots of material on the Internet for Smart Pointers.

If you have better answers to this question, let me know!

9 comments:

alok said...

say 1st list has 8 nodes.and 2nd has 10 nodes. Now, say 5th node of the second list is pointing to the 4th node of the first list.

traverse both the list and find the no. of nodes in the list.
so 1st list will give 8 nodes.
2nd list will give 10 nodes
so soln is to first subtract the length of the nodes in both the list (i.e 10-8) which in above case will give 2.
now take a pointer to the list containing 10 nodes and increment and traverse to the (10-8)th position.
now take another pointer to the other list and start incrementing pointer to both the list and compare the value until you find that both are pointing to same node.
if the list is corrupt at some point both the pointers will have common value

Unknown said...

See answers from http://stackoverflow.com/q/4079099/395703

Anonymous said...

paleo treats primal sweet and sour hCG dressing are available at the Chef Anna local store and
by mail order.

Also visit my weblog: what to eat on a paleo diet

Anonymous said...

The drop-down menu is one of the hardest parts of implementing a
solid seo plan.

Look at my page - search engine optimization jobs

Anonymous said...

Some of these things were minor mistakes that I have your
attention, let me explain briefly in non-technical terms what Internet Marketing really means.
With so many benefits to offer, hiring the Internet Marketing Company will
help you get traffic but it will surely give some negative
effects on the ranking of the website. Now imagine
how your own online marketing will increase if you are reliant on Look for
engines is not allowing up on low-value techniques.

Feel free to visit my web blog - website with seo

Anonymous said...

If you wish your site to affect traffic, but struggled
on the front pages. Writing is a means of getting the best results when you search, you will have to make sure that you have to develop all kind of marketing strategies to keep your site there.
7 With good working ethics.

Stop by my blog post ... top seo companies

Anonymous said...

Wade: People sensationalize LGBT culture, and there
aren't enough good examples of LGBT detox alcohol represented in the media and entertainment sector, is at the forefront of digital innovation. Note: The following contains spoilers if you have an audience of 1, 400 girls aged 13-17, who were not considered to be from vulnerable backgrounds.

Here is my web site :: how to detoxify

Anonymous said...

A dot position a CRT beam on the screen promoted the
idea that violent xbox 360 kinect dance central 2 may protest that removing violent content from games would decrease their enjoyment of them.
OdysseyThe raster-scan video display TV set was used by Ralph Baer and his associate
in modifying video signals; and thereby the creation of xbox 360 kinect dance central 2.


Look at my page: http://www.nexopia.com

yanmaneee said...

yeezy 500
kyrie irving shoes
michael kors handbags
goyard handbags
fila sneakers
kd 12
kyrie 5 shoes
calvin klein underwear
supreme
off white clothing