printf() print arguments in reversed order

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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!



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux