1

I have two structs in my code

struct Node
{
int no;
Node* next1;
Node* next2;
char path1;
char path2;
};

struct NodeSet
{
Node* entry;
Node* exit;
};

and a deque like

deque<NodeSet> nsQueue[100]

The problem is when runs to: nsQueue[level+1].push_back(ns) before execution: +

ns  {entry=0x0026f5a0 {no=2 next1=0x0026f350 {no=3 next1=0x002999e8 {no=4 next1=0x00299a38 {...} next2=0xcdcdcdcd {...} ...} ...} ...} ...} NodeSet

after execution: +

ns  {entry=0x0026f5a0 {no=2 next1=0x0026f350 {no=-858993460 next1=0x00000000 {no=??? next1=??? next2=??? ...} ...} ...} ...}    NodeSet

Why do the values change? Thanks for help.

999k
  • 6,257
  • 2
  • 29
  • 32

2 Answers2

4

Because I'll bet you didn't call begin() and end() again after calling push_back()

deque invalidates its iterators after calling push_back()

Why does push_back or push_front invalidate a deque's iterators?

But also I can't prove that without seeing your code.

Edit: Or you're doing something strange because you have 100 queues, which isn't doing what you think its doing.

Community
  • 1
  • 1
Salgar
  • 7,687
  • 1
  • 25
  • 39
2

I suspect that the list of Node objects that the ns NodeSet pointed to was no longer valid (ie., those objects were no alive anymore) for some reason, so the memory is being reused by the push_back() call. One clue is that the call to push_back() trashed the memory, another clue is that some of the memory dump includes:

  • after the push_back(): no=-858993460 which is equivalent to no=0xCCCCCCCC. That pattern is often used by MS compilers to initialize automatic variables (that aren't explicitly initialized by code) to help detect using initialized variables.

Also, before the push_back(), the dump shows that next2=0xcdcdcdcd. That pattern is used by the debug heap runtime to fill 'clean memory' which indicates memory that has been allocated but not written to by the application since the allocation. This might not be a bug (it's perfectly valid to not write to allocated memory, as long as you don't otherwise use it), but it's an indication that at least some of the objects in the list of Node structs might not quite be right.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Yes, I just want to know why after push_back "next1=0x0026f350 {no=3 next1=0x002999e8" becomes "next1=0x0026f350 {no=-858993460 next1=0x00000000". You mention that "no=-858993460" is often used by MS compilers to initialize automatic variables, but I don't declare a new variable here. – user2289677 May 10 '13 at 07:28
  • @user2289677: if the memory being used by the `Node` at `0x0026f350` is no longer valid, then it night be repurposed for other uses - perhaps a local variable in `push_back()` that never gets used? Without a SSCCE as Joachim asked for, I'm just making some educated guessed based on the tiny bit of information about the state of the objects given in the question - there's absolutely no info given about how the `Node` list is created. But based on the debugger output I can guess that at least some of the objects on that list are dead. – Michael Burr May 10 '13 at 07:42
  • Thanks very much, I found the cause. You are right, create Node by new can fix it. Thanks again. – user2289677 May 10 '13 at 08:07