does gdb lie? pointers to data changing on me...
oh man, am i having problems today! I have the following function as part of a red black tree program I am writing. The program is not qorking quite right. It compiles fine with gcc, no errors. But I found an issue with GDB in the following code.
Code:
208 // This function performs a left rotation on nodes
209
210 void leftrotate(struct node** root, struct node** newnode)
211 {
212 struct node* y;
213
214 y=(*newnode)->right;
215 (*newnode)->right=y->left;
216 if(y->left)
217 y->left->parent=(*newnode);
218 (y->parent)=((*newnode)->parent);
219 if(!(*newnode)->parent)
220 (*root)=y;
221 else
222 {
223 if(*newnode == (*newnode)->parent->left)
224 (*newnode)->parent->left=y;
225 else
226 (*newnode)->parent->right=y;
227 }
228 y->left=*newnode;
229 (*newnode)->parent=y;
230
231 return;
232 }
... GDB output
Code:
Breakpoint 1, leftrotate (root=0xbffff6cc, newnode=0x804a410) at main.c:214
214 y=(*newnode)->right;
(gdb) p newnode->parent->data
$1 = 9
(gdb) p newnode->data
$2 = 12
(gdb) step
215 (*newnode)->right=y->left;
(gdb)
216 if(y->left)
(gdb)
218 (y->parent)=((*newnode)->parent);
(gdb) p newnode->parent->data
$3 = 9
(gdb) p newnode->data
$4 = 12
(gdb) p y->data
$5 = 16
(gdb) step
219 if(!(*newnode)->parent)
(gdb) p newnode->parent->data
Cannot access memory at address 0x0
(gdb) p newnode->data
$6 = 9
(gdb) p y->data
$7 = 16
I don't understand why the newnode->data and newnode->parent->data seem to move one level up the tree since I'm not doing anything to the node the newnode poiter points at. Am I correct that GDB doesn't execute the line that it is showing you until you step? I just don't see why my values are changing here as the only thing I tried to do was point y's parent pointer to same thing newnode's parent pointer points at...
Does GDB tell lies? Sorry if I'm becoming a nuisance. Oh, below is my tree at this point... a little sloppy. Sorry.
Thanks for any help!
dinjas
Code:
root
|
V
9
/ \
3 12
/ \
2 16
\
19