Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ pointer assignment statement doesn't seem to work / do anything

I have the following method in a template class (a simple FIFO queue) and while GDB debugging, I found that the statement to reassign the pointer 'previous' to 'current' seems to do nothing. previous starts as NULL and current is not NULL when this statement is executed, yet previous remains as NULL. Has anyone seen anything like this before?

inline int search(QueueEntry<T> *current,QueueEntry<T> *previous, unsigned long long t)
{
  while(current && !(current->getItem()->equals(t)))
  {
    previous = current; //**this line doesn't seem to work**
    current = current->getNext();
  }
  if(current)
    return 1;
  return 0;    
}
like image 917
Elly Avatar asked Dec 22 '22 11:12

Elly


1 Answers

The assignment is optimized away by the compiler because it doesn't have any effect on the function's behavior. You reassign previous every time through the loop, but do nothing else with it.

The bug, if any, is in the rest of the function.

like image 196
Fred Foo Avatar answered Dec 24 '22 00:12

Fred Foo