Access denied while trying to push_back to a list<int> in a loop

71 Views Asked by At

While studying C++ and playing around with lists, I started experiencing some weird behavior, which I was able to isolate to the following piece of code:

#include <list>

int main() {
    std::list<int> ls;
    for(int i = 0; i < 10; ++i) {
        ls.push_back(1);
    }
}

I am able to compile this code but, when I try to run it, "Access denied" is printed to the prompt and the program is terminated. Sometimes, when I run the executable, the .exe file will disappear from the file system and I will be unable to create a new file with the same name in that folder, even if I delete and recreate the folder.

Some notes:

  • The minimum number of iterations on the loop that I am able to reproduce this error is 10. Any lower than that the program runs fine.
  • I am running this on Windows 10, using the Microsoft compiler.
  • The error only occurs if I specify the /MT option. If I specify /MTd, /MD or /MDd the program runs fine. (Failing to specify any of these options also causes the problem. That's how I found the problem in the first place)
  • If I try to specify /Z7, /Zi or /ZI the problem does not occur. (So I cannot debug it)
  • I have tried disassembling the code generated by compiling with /MT and /MD to see if I could spot any differences. There were some small differences in the generated codes, but I couldn't tell if they were responsible for this behavior.(I know almost nothing of assembly)
  • Microsoft Compiler version: Microsoft (R) C/C++ Optimizing Compiler Versão 19.10.25019 para x86 (my windows is in Portuguese)
  • If I place an std::cout anywhere in the code the problem stops.
  • If I use other containers, the problem does not occur.

Is this a bug in the compiler or is there something I am missing?

0

There are 0 best solutions below