0
#include <vector>
#include <set>
using namespace std;
vector<set<int> > g_vec;
int main()
{
    g_vec.push_back({ 1 });
    g_vec.push_back({ 1 });
    g_vec.emplace_back(g_vec[0]);//Access violation at address 0xDDDDDDE1
    //g_vec.push_back(g_vec[0]);
    return 0;
}

I can compile this piece of code on ideone :http://ideone.com/BmYzMR.

But vs2015 gives me an runtime error .(Access violation at address 0xDDDDDDE1) enter image description here enter image description here

Is this undefined behaviour ?

How can I make my code work on vs2015?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
iouvxz
  • 89
  • 9
  • 27
  • 6
    If you get a runtime error, it must have compiled successfully. – molbdnilo Sep 23 '16 at 14:04
  • 4
    Looks like an MSVS bug as [it is required to work](http://stackoverflow.com/questions/18788780/is-it-safe-to-push-back-an-element-from-the-same-vector) – NathanOliver Sep 23 '16 at 14:04
  • @NathanOliver The example you give is a vector This is a vector>. He's constructing a set with a single integer as the construction parameter in the line `push_back({1});` [at least I think that's what's happening]. – Dale Wilson Sep 23 '16 at 14:07
  • Yep, MSVC bug. STL once posted on reddit that this is hard to implement, and apparently no one at Microsoft got around to fix it so far. – Baum mit Augen Sep 23 '16 at 14:08
  • @NathanOliver That's `push_back`, `push_back` is working (try it). – Hatted Rooster Sep 23 '16 at 14:08
  • @DaleWilson How does that make any difference? – Baum mit Augen Sep 23 '16 at 14:09
  • @GillBates I know. I posted that link because in it all functions that insert into the vector must be able to insert an element from the vector into itself. cc DaleWilson – NathanOliver Sep 23 '16 at 14:09
  • @GillBates `emplace_back` should work too. – Baum mit Augen Sep 23 '16 at 14:09
  • 2
    @BaummitAugen I think this is this discussion: https://www.reddit.com/r/cpp/comments/vog1p/a_commonly_unknown_stdvector_pitfall/ – marcinj Sep 23 '16 at 14:11
  • Unfortunately the answer is either post a bug report or find an existing one and make sure it is being worked on. As a work around you can create a copy and them emplace that back but that is going to have a performance penalty. – NathanOliver Sep 23 '16 at 14:29

0 Answers0