How to make sure dereference to vector is valid

I have this piece of code in parts of my path finding algorithm

1
2
3
4
5
6
7
8

for( int head; head < q.size(); ++ head ){
    walk& w = q[head];
    
    // do manything with w
    if( some_condition ) q.push_back( walk( w.x + 1, w.y, head ) );
}


However I notice that sometimes w is cannot be dereferenced. It can but it throws junk number at me. Perhaps the vector is changing it size and move the whole array to a different location. Is there anyway to make sure that w is always valid ?

I just want to use w because of shorter typing and cleaner look not because of performance. I also refrain from using macro.
Perhaps the vector is changing it size and move the whole array to a different location
Yes. Any push_back has potential to invalidate all references and iterators to the vector content.

Is there anyway to make sure that w is always valid ?
Yes. Do not use any iterator invalidating operations while you using your reference.
For example you can make sure that your vector has large enoug capacity that push_backs will not lead to reallocation while reference is alive.
I am sorry for the late reply.

I will complete my question.
Is there anyway to make sure that w is always valid even if there is invalidating operations ?

I sure can reserve but I am not very sure that my algorithm is bug free. It might surpass the reserve limit because if the bug and the error is not within the dereferencing but causes the deferencing to be wrong when the vector reallocate when it shouldn't because of the bug.. And then I will be confused because there isn't suppose to be a bug there but somewhere else but the bugger said otherwise

and without macro please ...

if not possible, I will probably copy instead of referencing it ...
Why not just replace w with q[head]?
I don't see how that code is invalid. Argument evaluations are sequenced before the function call.

I suspect you are getting a junk number for some other reason. Or the code you posted doesn't accurately represent what is in your loop.

What's a "walk"?
Last edited on
@Duaos
walk is just a struct
1
2
3
4
5
6
7
struct walk {
	int x;
	int y;
	int walk_count;
	int estimate_cost;
	int back;
};

for A* algorithm...

Does it really matter what's 'walk' ?

I am pretty sure it's because of the push_back because the number becomes junk only after the push_back... ( I've tested it )

@naraku9333
I could do that but you know the code looks long and it's tiring to type that way
That's why I am dereferencing it and this problem occurs.
Is there anyway to make sure that w is always valid even if there is invalidating operations ?
Yes. Reassign it each time invalidation happens. However you cannot rebind references.
Use iterator and reassign it after each potentially invalidating operation:
1
2
3
4
auto it = std::advance(q.begin(), head);
//...
q.push_back(walk( it->x + 1, it->y, head );
it = std::advance(q.begin(), head);

OPs is claiming that the language is doing something the language forbids. I seriously doubt that that's the problem.

The code presented does not exhibit the problem. Therefore the code presented so far is not representative of the code being used to generate the problem.

Unless you are willing to show us your actual code, we cannot help diagnose the problem. Because what's happening is you have already diagnosed the problem yourself (incorrectly), and want us to verify it to you by telling us how to 'fix' it.

The push_back isn't screwing up. You are getting a junk number for some other reason.
Last edited on
However I notice that sometimes w is cannot be dereferenced
I am pretty sure it's because of the push_back because the number becomes junk only after the push_back
Code is incomplete, but it possibly that this is a reallocation problem.

Full code still needed, however. Only with it we can either prove or disprove this theory.
Topic archived. No new replies allowed.