Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Corrupt pointer in a linked list

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

like image 742
neelima Avatar asked Nov 02 '10 15:11

neelima


People also ask

How do you find a corrupted node in a linked list?

If you can do anything to the linked list, what you can do is to calculate the checksum of each element and store it on the element itself. This way you will be able to detect corruption even if it's a single bit error on the element.

Are pointers used in linked list?

A linked list is a list constructed using pointers. A linked list is not fixed in size but can grow and shrink while your program is running. This chapter shows you how to define and manipulate linked lists, which will serve to introduce you to a new way of using pointers.

How do pointers work in linked lists?

The pointer always points to the next member of the list. If the pointer is NULL, then it is the last node in the list. A linked list is held using a local pointer variable which points to the first item of the list. If that pointer is also NULL, then the list is considered to be empty.


1 Answers

Introduce a magic value in your node structures. Initialize it upon new node allocation. Before every access, check if the node structure that the pointer points to contains the valid magic. If the pointer points at an unreadable data block, your program will crash. For that, on Windows there's API VirtualQuery() - call that before reading, and make sure the pointer points at readable data.

like image 69
Seva Alekseyev Avatar answered Nov 11 '22 02:11

Seva Alekseyev