user2289677
user2289677

Reputation: 33

deque::push_back() in c++

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.

Upvotes: 1

Views: 1072

Answers (2)

Michael Burr
Michael Burr

Reputation: 340178

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.

Upvotes: 2

Salgar
Salgar

Reputation: 7775

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.

Upvotes: 4

Related Questions