printf() print arguments in reversed order
topher
topherchen@gmail.com
Thu Dec 8 06:43:00 GMT 2016
Hello,
I'm trying the following code and see some behavior I don't understand.
#include <string>
#include <cstdio>
#include <sstream>
using namespace std;
struct StaticName {
string getName() {
static int i = 0;
name.clear();
ostringstream oss;
oss << ++i;
name += oss.str();
return name;
}
static string name;
};
string StaticName::name = "";
int main(int argc, char* argv[]) {
StaticName sn;
printf("name1=%s, name2=%s, name3=%s\n",
sn.getName().c_str(), sn.getName().c_str(), sn.getName().c_str());
return 0;
}
The executable built by g++ 4.8.5 prints "name1=3, name2=2, name3=1".
However, executable built by clang++ 3.8.0 prints "name1=1, name2=2,
name3=3", which is what I expected.
Is there any undefined behavior in my code?
Thanks!
More information about the Gcc-help
mailing list