2

I am new to STL/C++11 programming, and am going through the motions to understand it.

And I am afraid I am stuck on what seems a trivial task, swapping string lists.

When I swap two lists of strings using the std::swap() method, I get no errors, but the list class member function, swap(), gives me an access violation:

#include<iostream>
#include<list>
#include<string>

using namespace std;

int main()
{
    list<string> superheroes {"Spidey", "Supes", "Bats"};
    list<string> supervillains {"Goblin", "Luthor", "Joker"};
    std::swap(superheroes, supervillains);  //swaps fine
    superheroes.swap(supervillains); //access violation
    return 0;
}

I am using Visual Studio Pro 2013.

chappjc
  • 30,359
  • 6
  • 75
  • 132
LoadRunner
  • 43
  • 4
  • 4
    Cannot reproduce. Could you post an SSCCE (http://www.sscce.org/)? – NPE Mar 01 '14 at 16:58
  • Your code is fine as shown here: http://ideone.com/UOrtmU we'll need more details that the above to figure out what is going on. – Brandon Mar 01 '14 at 17:09
  • Compiles fine on gcc 4.8.2. – Felix Glas Mar 01 '14 at 17:10
  • Ok I edited it. I am using visual studio pro 2013 – LoadRunner Mar 01 '14 at 17:14
  • Thanks a lot guys. I have no idea what the problem is, but I started a new project with the same code, and was unable to reproduce the problem myself. Corrupted file I guess. But nonetheless, thank you. – LoadRunner Mar 01 '14 at 17:35
  • 2
    *" I am using visual studio pro 2013"* <- this. There are bugs related to list initialization in VS2013. Try to use `push_back` instead of list-init. I'll see if I can find the bug reports. – dyp Mar 01 '14 at 21:37
  • 4
    See http://connect.microsoft.com/VisualStudio/feedback/details/807419/initializer-lists-leaking-memory and http://stackoverflow.com/q/20297191/420683 Please try without braced-init-lists, e.g. with `list superheroes; superheroes.push_back("Spidey"); /* and so on */` and similarly for `supervillains`. – dyp Mar 01 '14 at 21:44
  • 1
    Will do dyp. Much appreciated. To think it's 2014, and MS hasn't ironed out 2011 c++ yet. Lol. Thanks. – LoadRunner Mar 02 '14 at 00:42

0 Answers0