I’m working on a WSL virtual machine with Ubuntu 22.04 and running the following code:
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> symbols;
symbols.push_back("a");
symbols.push_back("b");
symbols.push_back("c");
symbols.push_back("d");
symbols.push_back("e");
symbols.push_back("f");
std::cout << "symbols:" << std::endl;
for (size_t i=0; i<symbols.size(); i++)
{
std::cout << "<" << symbols[i] << "> " ;
}
std::cout << std::endl;
std::string res;
std::cout << symbols.size() << std::endl;
for (size_t i; i< symbols.size(); i++)
{
res += symbols[i] + " ";
std::cout << res <<std::endl;
}
std::cout << "result: <<" << res << ">>" << std::endl;
}
The results differ between the program calls, sometimes getting the expected result:
symbols:
<a> <b> <c> <d> <e> <f>
6
a
a b
a b c
a b c d
a b c d e
a b c d e f
result: <<a b c d e f >>
and sometimes the iteration not taking place at all:
symbols:
<a> <b> <c> <d> <e> <f>
6
result: <<>>
I’m using g++ 11.4.0 as the compiler. The question is: could this behavior be a bug of the compiler, or am I just blind to some obvious mistake I’m making?
(I have tried switching the compiler to g++ 12.3.0, however the bug persists)
4