UPD: It was answered in Errichto's comment
I was coding a build function for persistent segment tree using vectors and I am getting Runtime error.
I replaced vector with array and it passes.
The code is just a few lines.
Code using vector (State: gives Runtime error): http://ideone.com/2c5tec
Code using arrays (State: Okay): http://ideone.com/WxBwJJ
I spent hours trying to spot the bug in the code and I removed all the code which is irrelevant to the bug to make it more simple to debug.
What really makes it more weird is when i removed those: "v[p].l=" and "v[p].r" it runs okay, here is it: http://ideone.com/alzUxP (State: Okay)
I will appreciate it very much if anyone told me why it keeps giving RTE.
Thanks very much.
I'm not sure about it but here's what I think/remember.
The thing is that when
vector
doubles its size (it must do it sometimes to contain more elements), it can be moved in the memory. So, when there isv[p].l=build(s,mid);
, first a place withv[p]
is remembered and thenbuild()
is run, where the size ofv
can be doubled and thusv[p]
won't longer be in the same place.Oh, yes you are right :)
I modified the code and stored the returned value in a variable then store it in v[p].l and worked : http://ideone.com/wIgY3x
Thanks you very very much Errichto :)
Alternatively, you could've called
reserve
function on the vector, so that it won't be reallocated during the call to thebuild
.yes, thank you eduardische
I guess i should be more careful when using vectors :)