Connecting Tech Pros Worldwide Help | Site Map

unexpected stream output with commas

  #1  
Old July 23rd, 2005, 05:51 AM
Kyle Kolander
Guest
 
Posts: n/a
Got a strange bug going on here...

// proper includes and additional code here
int x = 2552123;
cout << x;

This results in output like this: 2,552,123
Same behavior with stringstreams.

How can this be happening?
This doesn't always happen... seems like only when we create our executable
with some of our shared libraries (.so).
It works correctly when in main() but not when in our class source files.
When linking with static libraries (.a) it works as expected.

We are developing on AIX with Visual Age 6.0 if that matters.

It's probably something we are doing incorrectly with our shared
libraries... any hints as to what could be causing this?

Thanks,
Kyle


  #2  
Old July 23rd, 2005, 05:51 AM
Howard
Guest
 
Posts: n/a

re: unexpected stream output with commas



"Kyle Kolander" <kkolander@hotmail.com> wrote in message
news:JZmle.5$M91.1@dfw-service2.ext.ray.com...[color=blue]
> Got a strange bug going on here...
>
> // proper includes and additional code here
> int x = 2552123;
> cout << x;
>
> This results in output like this: 2,552,123[/color]

I take it you don't want the commas? Or do you want periods there? (You
don't specify what it is you actually expected.)
[color=blue]
> Same behavior with stringstreams.
>
> How can this be happening?
> This doesn't always happen... seems like only when we create our
> executable
> with some of our shared libraries (.so).
> It works correctly when in main() but not when in our class source files.
> When linking with static libraries (.a) it works as expected.
>
> We are developing on AIX with Visual Age 6.0 if that matters.
>
> It's probably something we are doing incorrectly with our shared
> libraries... any hints as to what could be causing this?
>
> Thanks,
> Kyle
>[/color]

I'd guess that the shared libraries are specifying locale information, and
that includes whether or not to use thousands separators (and what to use
for them). Check the shared library code (and the settings used to build
them, perhaps?), and see if they're making such specifications. If not,
then perhaps you need to add code to tell them to NOT use the separators?
I'm not familiar with how to do such things, but this should help point you
in the right direction, at least. A Google search might help more.

-Howard

[color=blue]
>[/color]


  #3  
Old July 23rd, 2005, 05:51 AM
Victor Bazarov
Guest
 
Posts: n/a

re: unexpected stream output with commas


Kyle Kolander wrote:[color=blue]
> Got a strange bug going on here...
>
> // proper includes and additional code here
> int x = 2552123;
> cout << x;
>
> This results in output like this: 2,552,123
> Same behavior with stringstreams.
>
> How can this be happening?
> This doesn't always happen... seems like only when we create our executable
> with some of our shared libraries (.so).
> It works correctly when in main() but not when in our class source files.
> When linking with static libraries (.a) it works as expected.
>
> We are developing on AIX with Visual Age 6.0 if that matters.
>
> It's probably something we are doing incorrectly with our shared
> libraries... any hints as to what could be causing this?[/color]

Default locale setting uses 'do_grouping' that causes 'thousands_sep'
character to be inserted into the output. I don't know much about those
things, but read about locales in general. Could it be your code uses
different locales if a certain .so is loaded. It must set the locale
to something instead of just leaving it unchanged. Read the documentation
for that library, especially the part where it describes locales.

V
  #4  
Old July 23rd, 2005, 05:51 AM
Kyle Kolander
Guest
 
Posts: n/a

re: unexpected stream output with commas


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:W4nle.77166$NC6.13739@newsread1.mlpsca01.us.t o.verio.net...[color=blue]
> Kyle Kolander wrote:[color=green]
> > Got a strange bug going on here...
> >
> > // proper includes and additional code here
> > int x = 2552123;
> > cout << x;
> >
> > This results in output like this: 2,552,123
> > Same behavior with stringstreams.
> >
> > How can this be happening?
> > This doesn't always happen... seems like only when we create our[/color][/color]
executable[color=blue][color=green]
> > with some of our shared libraries (.so).
> > It works correctly when in main() but not when in our class source[/color][/color]
files.[color=blue][color=green]
> > When linking with static libraries (.a) it works as expected.
> >
> > We are developing on AIX with Visual Age 6.0 if that matters.
> >
> > It's probably something we are doing incorrectly with our shared
> > libraries... any hints as to what could be causing this?[/color]
>
> Default locale setting uses 'do_grouping' that causes 'thousands_sep'
> character to be inserted into the output. I don't know much about those
> things, but read about locales in general. Could it be your code uses
> different locales if a certain .so is loaded. It must set the locale
> to something instead of just leaving it unchanged. Read the documentation
> for that library, especially the part where it describes locales.
>
> V[/color]

Thanks guys... I started suspecting locale was the problem after a quick
search of google.
I have also only read a bit about locale settings and never used them.
I'll look into your suggestions.

Thanks again,
Kyle


  #5  
Old July 23rd, 2005, 05:51 AM
Kyle Kolander
Guest
 
Posts: n/a

re: unexpected stream output with commas


[color=blue]
> Default locale setting uses 'do_grouping' that causes 'thousands_sep'
> character to be inserted into the output. I don't know much about those
> things, but read about locales in general. Could it be your code uses
> different locales if a certain .so is loaded.[/color]

I guess we don't understand how the locale could vary based on whether
we use a .so or .a library, because the .a version works and the .so does
not.
Could you please shed a bit more light on this?

Thanks in advance,
Kyle





  #6  
Old July 23rd, 2005, 05:51 AM
Victor Bazarov
Guest
 
Posts: n/a

re: unexpected stream output with commas


Kyle Kolander wrote:[color=blue][color=green]
>>Default locale setting uses 'do_grouping' that causes 'thousands_sep'
>>character to be inserted into the output. I don't know much about those
>>things, but read about locales in general. Could it be your code uses
>>different locales if a certain .so is loaded.[/color]
>
>
> I guess we don't understand how the locale could vary based on whether
> we use a .so or .a library, because the .a version works and the .so does
> not.
> Could you please shed a bit more light on this?[/color]

I could if I knew the details. My _guess_ would be that there is some
kind of initialisation code that .so executes when loaded, which might
contain setting the locale. You'll need to ask in a newsgroup where .so
and .a are topical, like a newsgroup for your OS.

I would also strongly recommend contacting the compiler vendor (IBM?).

V
  #7  
Old July 23rd, 2005, 05:51 AM
Howard
Guest
 
Posts: n/a

re: unexpected stream output with commas



"Kyle Kolander" <kkolander@hotmail.com> wrote in message
news:2mnle.7$M91.6@dfw-service2.ext.ray.com...[color=blue]
>[color=green]
>> Default locale setting uses 'do_grouping' that causes 'thousands_sep'
>> character to be inserted into the output. I don't know much about those
>> things, but read about locales in general. Could it be your code uses
>> different locales if a certain .so is loaded.[/color]
>
> I guess we don't understand how the locale could vary based on whether
> we use a .so or .a library, because the .a version works and the .so does
> not.
> Could you please shed a bit more light on this?
>
> Thanks in advance,
> Kyle[/color]

You haven't said where you ger those libraries from. Are you creating them
yourself, or are they some pre-existing libraries? If you're creating them,
it may be that the build settings (makefile, or whatever) has some control
over that aspect. If they're pre-existing, then perhaps whoever built them
might know the difference.

In any case, specifics about library types and the differences between them
are not part of the C++ language specification, and are platform-dependant.
You'd probably get more information in a newsgroup for your operating system
and/or compiler.

-Howard


  #8  
Old July 23rd, 2005, 05:51 AM
Kyle Kolander
Guest
 
Posts: n/a

re: unexpected stream output with commas



"Howard" <alicebt@hotmail.com> wrote in message
news:Bunle.834567$w62.30096@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
>
> "Kyle Kolander" <kkolander@hotmail.com> wrote in message
> news:2mnle.7$M91.6@dfw-service2.ext.ray.com...[color=green]
> >[color=darkred]
> >> Default locale setting uses 'do_grouping' that causes 'thousands_sep'
> >> character to be inserted into the output. I don't know much about[/color][/color][/color]
those[color=blue][color=green][color=darkred]
> >> things, but read about locales in general. Could it be your code uses
> >> different locales if a certain .so is loaded.[/color]
> >
> > I guess we don't understand how the locale could vary based on whether
> > we use a .so or .a library, because the .a version works and the .so[/color][/color]
does[color=blue][color=green]
> > not.
> > Could you please shed a bit more light on this?
> >
> > Thanks in advance,
> > Kyle[/color]
>
> You haven't said where you ger those libraries from. Are you creating[/color]
them[color=blue]
> yourself, or are they some pre-existing libraries? If you're creating[/color]
them,[color=blue]
> it may be that the build settings (makefile, or whatever) has some control
> over that aspect. If they're pre-existing, then perhaps whoever built[/color]
them[color=blue]
> might know the difference.
>
> In any case, specifics about library types and the differences between[/color]
them[color=blue]
> are not part of the C++ language specification, and are[/color]
platform-dependant.[color=blue]
> You'd probably get more information in a newsgroup for your operating[/color]
system[color=blue]
> and/or compiler.
>
> -Howard
>
>[/color]

Thanks again... I posted to comp.unix.programmer and comp.unix.aix,
hopefully they will have advice.
They are our own libraries (lots of them written by lots of different
people) ;)
I'll check on the Makefiles.

Thanks,
Kyle


  #9  
Old July 23rd, 2005, 05:51 AM
Kyle Kolander
Guest
 
Posts: n/a

re: unexpected stream output with commas



"Howard" <alicebt@hotmail.com> wrote in message
news:Bunle.834567$w62.30096@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
>
> "Kyle Kolander" <kkolander@hotmail.com> wrote in message
> news:2mnle.7$M91.6@dfw-service2.ext.ray.com...[color=green]
> >[color=darkred]
> >> Default locale setting uses 'do_grouping' that causes 'thousands_sep'
> >> character to be inserted into the output. I don't know much about[/color][/color][/color]
those[color=blue][color=green][color=darkred]
> >> things, but read about locales in general. Could it be your code uses
> >> different locales if a certain .so is loaded.[/color]
> >
> > I guess we don't understand how the locale could vary based on whether
> > we use a .so or .a library, because the .a version works and the .so[/color][/color]
does[color=blue][color=green]
> > not.
> > Could you please shed a bit more light on this?
> >
> > Thanks in advance,
> > Kyle[/color]
>
> You haven't said where you ger those libraries from. Are you creating[/color]
them[color=blue]
> yourself, or are they some pre-existing libraries? If you're creating[/color]
them,[color=blue]
> it may be that the build settings (makefile, or whatever) has some control
> over that aspect. If they're pre-existing, then perhaps whoever built[/color]
them[color=blue]
> might know the difference.
>
> In any case, specifics about library types and the differences between[/color]
them[color=blue]
> are not part of the C++ language specification, and are[/color]
platform-dependant.[color=blue]
> You'd probably get more information in a newsgroup for your operating[/color]
system[color=blue]
> and/or compiler.
>
> -Howard
>
>[/color]

Perhaps there is a new twist to this odd behavior...
We added these two lines of code immediately preceding our output of the
int:

string s = cout.getloc().name();
cout << s;

the printout was "C" in both spots (one of which works, and the other does
not).
So it looks like the locale is not being changed. How can the locale be
"C", which is the Default: ANSI-C convention, and still print out numbers
with commas?

So I guess my question would be if there is any other way that streams could
be manipulated into formatting numerics this way?

Thanks,
Kyle


  #10  
Old July 23rd, 2005, 05:51 AM
Alan Johnson
Guest
 
Posts: n/a

re: unexpected stream output with commas


Kyle Kolander wrote:[color=blue]
> So I guess my question would be if there is any other way that streams could
> be manipulated into formatting numerics this way?
>
> Thanks,
> Kyle
>
>[/color]

The following program demonstrates the only way I know of to create the
behavior you describe. Although it seems unlikely that anyone has done
this in your code, since the locale name is still "C" (when you create a
locale like this, it does not have a name).

-Alan

#include <iostream>
#include <string>
#include <locale>

class group_thousands : public std::numpunct<char>
{
protected :
virtual std::string do_grouping() const
{
return "\3" ;
}
} ;

int main()
{
std::cout.imbue(std::locale(std::cout.getloc(), new group_thousands())) ;

std::cout << 123456789 << std::endl ;
}

  #11  
Old July 23rd, 2005, 05:52 AM
Kyle Kolander
Guest
 
Posts: n/a

re: unexpected stream output with commas


Alan Johnson wrote:[color=blue]
> Kyle Kolander wrote:
>[color=green]
>> So I guess my question would be if there is any other way that streams
>> could
>> be manipulated into formatting numerics this way?
>>
>> Thanks,
>> Kyle
>>
>>[/color]
>
> The following program demonstrates the only way I know of to create the
> behavior you describe. Although it seems unlikely that anyone has done
> this in your code, since the locale name is still "C" (when you create a
> locale like this, it does not have a name).
>
> -Alan
>
> #include <iostream>
> #include <string>
> #include <locale>
>
> class group_thousands : public std::numpunct<char>
> {
> protected :
> virtual std::string do_grouping() const
> {
> return "\3" ;
> }
> } ;
>
> int main()
> {
> std::cout.imbue(std::locale(std::cout.getloc(), new
> group_thousands())) ;
>
> std::cout << 123456789 << std::endl ;
> }
>[/color]

As it turns out, we were able to eliminate the problem by removing calls
to a performance library called HPM (Hardware Performance Monitor)
Toolkit from IBM. Go figure... *sigh*. Thanks to all for your help!

Kyle
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 08:57 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 07:46 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 03:55 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 5 November 14th, 2005 12:36 PM