Hacker News new | ask | show | jobs
by satbyy 3417 days ago
Just hit 'Run' here: http://rextester.com/BDGO47890

You can see the program crash

1 comments

Nice, but the example crashes because you take a reference to uninitialized vector, if you initialize it and grow the vector I found that it will either keep "working fine" if you don't expect the data to change or randomly give garbage data - depending on compiler/stdlib - especially since the time when the buffer grows is implementation defined.

Here is a better example : http://rextester.com/MAJDQ8375

On VC++ it will return : Reference is 0 but value is 9

On clang it will return : Reference is 32579632 but value is 9

So let's say I wasn't mutating the 0 index, VC example would work fine at runtime and then clang would suddenly start giving garbage results. This has caused me week of debugging in a big code base.

Thanks for the catch!