problem with linklist

S

sunilsomanna

template<class T>
void TimerDLList<T>::addFirst(TimerItem<T>* const itemPtr)
{
// suppose itemPtr is not pointing to NULL

itemPtr->prev = (TimerItem<T>*) NULL;

head = (TimerItem<T>*)NULL;

if (tail == (TimerItem<T>*)NULL ) // nothing in the link list
{
tail = itemPtr;
itemPtr->next = (TimerItem<T>*) NULL;
}
else
{
itemPtr->next = head;
head->prev = itemPtr;
}

head = itemPtr;

}

there seems to be some problem in head->prev = itemPtr.
There seems to be some corrution at this point.
PLEASE HELP.......
seems like head->prev is accessing a wrong address.
 
N

Neelesh Bodas

template<class T>
void TimerDLList<T>::addFirst(TimerItem<T>* const itemPtr)
{
// suppose itemPtr is not pointing to NULL

itemPtr->prev = (TimerItem<T>*) NULL;

head = (TimerItem<T>*)NULL;

if (tail == (TimerItem<T>*)NULL ) // nothing in the link list
{
tail = itemPtr;
itemPtr->next = (TimerItem<T>*) NULL;
}
else
{
itemPtr->next = head;
head->prev = itemPtr;

What are you exactly trying to do here? since head is a null pointer,
dereferencing it will lead to undefined behavior.
 
R

Rui Maciel

there seems to be some problem in  head->prev = itemPtr.
There seems to be some corrution at this point.
PLEASE HELP.......
seems like head->prev is accessing a wrong address.

The code that you posted doesn't look like homework. If that's the case, why
exactly are you trying to hand-write a brand new linked list implementation
instead of using one of STL's many container classes?


Rui Maciel
 
R

Rui Maciel

there seems to be some problem in  head->prev = itemPtr.
There seems to be some corrution at this point.
PLEASE HELP.......
seems like head->prev is accessing a wrong address.

The code that you posted doesn't look like homework. If that's the case, why
exactly are you trying to hand-write a brand new linked list implementation
instead of using one of STL's many container classes?


Rui Maciel
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

template<class T>
void TimerDLList<T>::addFirst(TimerItem<T>* const itemPtr)
{
// suppose itemPtr is not pointing to NULL

itemPtr->prev = (TimerItem<T>*) NULL;

head = (TimerItem<T>*)NULL;

if (tail == (TimerItem<T>*)NULL ) // nothing in the link list
{
tail = itemPtr;
itemPtr->next = (TimerItem<T>*) NULL;
}
else
{
itemPtr->next = head;
head->prev = itemPtr;

head == NULL here, you made it so a bit up. By the way, we use 0 and not
NULL in C++.
 
A

Andre Kostur

head == NULL here, you made it so a bit up. By the way, we use 0 and not
NULL in C++.

Not necessarily true (I still prefer to use NULL, I like the visual
reminder that I'm talking about a pointer and not an int. When 'nulptr'
becomes Standard, I'll be really happy. However, we do tend to not ever
cast NULL (or 0) to a specific pointer type. There should be no need to.
 
S

sunilsomanna

The code that you posted doesn't look like homework. If that's the case, why
exactly are you trying to hand-write a brand new linked list implementation
instead of using one of STL's many container classes?

Rui Maciel

This is an existing piece of code. i dont have the option of using any
other implementation, i have to fix this. While implementing this in a
loop with a few other functions, it was seen that head got an invalid
address (this happened only once and i am not able to reproduce the
issue)
 
J

James Kanze

[...]
By the way, we use 0 and not
NULL in C++.

Who is "we"? I've worked in a number of different places using
C++, and the coding guidelines have always required the use of
NULL, rather than 0. Neither is perfect, but NULL better
expresses the intent, and makes the code more readable.
(Traditionally, some people have objected to NULL, because it
could accidentally be used as an integer---0 obviously can be
used as an integer, too, but you expect that when you see 0.
Now that compilers warn in such cases, however, that argument is
sort of irrelevant.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top