ltoa Behavior

I am working with the following code:

#include <stdlib.h>
#include <string>
#include <iostream>

using std::cout;
using std::endl;
using std::flush;

int main()
{
long lng1 = 123;
long lng2 = 4567;
cout<<ltoa(lng1)<<ltoa(lng2)<<endl<<flush;
return 0;
}

Instead of receiving the output 1234567, I am receiving 1234123. This is on HP-UX 11.11. Another server, with the same OS, is producing the correct output. Is there a setting that I am missing? What is the cause of this problem?

From the ltoa man page

 WARNINGS
      The return values for ltostr(), ultostr(), ltoa() and ultoa() point to
      data whose content is overwritten by subsequent calls to these
      functions by the same thread.

You're referencing the same place in memory twice... therefore you're experiencing undefined behavuior - sometimes it works, other times not.

Thank you. That really helps in knowing the difference between the two systems.