Sigmathaar said:
Yes, Blink and Flink refer to the backward and foward links, my problem
is that it's the first time I'm dealing with them and it's also been a
littel while since I last coded anything in C or C++. Since it's
difficult to help me without any code to look I'll give the part who's
giving me problems.
This is the struct of the list :
typedef struct _LIST_ENTRY {
struct _LIST_ENTRY *Flink;
struct _LIST_ENTRY *Blink;
} LIST_ENTRY, *PLIST_ENTRY, *RESTRICTED_POINTER PRLIST_ENTRY;
Don't use leading underscores followed by capital letter, ever. That's
reserved for the "implementation" (i.e., the compiler/library vendor).
And prefer to only use all capital letters for macros.
That's also an old C construct. Use C++ instead:
struct ListEntry
{
ListEntry* FLink;
ListEntry* BLink; // although Next and Previous would be more readable!
};
// this is your pointer typedef, but useless in my ompinion,
// since you can always use ListEntry*, which takes the same amount of
typing
typedef ListEntry* PListEntry;
These are the variables I'm using :
static LIST_ENTRY m_CacheListHead;
LIST_ENTRY* pEntry;
LIST_ENTRY* pEntryNext;
USER_INFO* pUser;
ListEntry* pEntry;
ListEntry* pEntryNext;
And here's an extract of the code I'm having problems with :
for ( pEntry = m_CacheListHead.Flink; pEntry != &m_CacheListHead;
pEntry = pEntryNext )
{
pUser = CONTAINING_RECORD( pEntry, USER_INFO, ListEntry );
pEntryNext = pEntry->Flink;
pEntry->Blink->Flink = pEntry->Flink;
pEntry->Flink->Blink = pEntry->Blink;
The code is suposed to go through pUser and free the memory used by
each entry on CacheListHead but I don't understand the last two lines
which for me seem to be useless. Can somebody explain me what are those
lines for?
If you are removing every ListEntry item (you don't show the rest of the
code, but it appears so), then those two lines are indeed useless. But
similar code is often used when removing a _single_ item from a list, in
order to tell the items on either side of the removed item who they should
now be pointing at. (Perhaps the coder simply did here what he/she does
whenever they remove a _single_ item from the list?)
However, unless this is a _circular_ linked list, where FLink and BLink are
never NULL, then they're likely to cause a crash or other Undefined
Behavior, since the BLink (previous) pointer for the first item would be
NULL, making the following line illegal:
pEntry->Blink->Flink = pEntry->Flink; // Can't access
pEntry->BLink->FLink if pEntry->BLink is NULL!
(A similar thing would happen at the end of the list, when pEntry->FLink is
NULL.)
-Howard