Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cout printing ints in hexadecimal instead of decimal

Reply
Thread Tools

cout printing ints in hexadecimal instead of decimal

 
 
Diwa
Guest
Posts: n/a
 
      10-01-2009
Our app uses cout to print ints

However on somedays, everything is in hexadecimal format

"ios::hex" or "hex" does not exist in our code anywhere.

Any enviromental variables whivch affect C++ iostreams

Or some library setting "hex"

Or is it the use of C style printfs in the code which confuses c++
iostream?
 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      10-01-2009
Diwa <(E-Mail Removed)> writes:

> Our app uses cout to print ints
>
> However on somedays, everything is in hexadecimal format
>
> "ios::hex" or "hex" does not exist in our code anywhere.
>
> Any enviromental variables whivch affect C++ iostreams
>
> Or some library setting "hex"
>
> Or is it the use of C style printfs in the code which confuses c++
> iostream?


On what days? Would that be on the 16th of each month?

--
__Pascal Bourguignon__
 
Reply With Quote
 
 
 
 
Francesco S. Carta
Guest
Posts: n/a
 
      10-01-2009
On 1 Ott, 17:26, Diwa <(E-Mail Removed)> wrote:
> Our app uses cout to print ints
>
> However on somedays, everything is in hexadecimal format
>
> "ios::hex" or "hex" does not exist in our code anywhere.
>
> Any enviromental variables whivch affect C++ iostreams
>
> Or some library setting "hex"
>
> Or is it the use of C style printfs in the code which confuses c++
> iostream?


As far as I know, the only two things that can (legally) affect the
formatting via any stream are passing a manipulator to them (such as
hex) or explicitly changing their flags.

Neither printf nor any environmental variable should affect such
formatting at all.

My pick is that some call to some external library is directly
affecting cout.

You can check out cout's flags after those calls to find the offending
call (adding a "cout << dec;" after that particular call, to solve the
problem) otherwise you can explicitly put cout in decimal mode every
time before outputting your ints - which should be the correct habit:
never make assumptions on the current state of a shared resource,
either check it or set it to the state you want it to be.

Hope that helps. Feel free to clarify and ask again if I misunderstood
your issue.

Have good time,
Francesco
--
Francesco S. Carta, hobbyist
http://fscode.altervista.org
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      10-02-2009
On Oct 1, 7:04 pm, "Francesco S. Carta" <(E-Mail Removed)> wrote:
> On 1 Ott, 17:26, Diwa <(E-Mail Removed)> wrote:


> > Our app uses cout to print ints


> > However on somedays, everything is in hexadecimal format


> > "ios::hex" or "hex" does not exist in our code anywhere.


> > Any enviromental variables whivch affect C++ iostreams


> > Or some library setting "hex"


This is the most likely cause.

> > Or is it the use of C style printfs in the code which
> > confuses c++ iostream?


> As far as I know, the only two things that can (legally)
> affect the formatting via any stream are passing a manipulator
> to them (such as hex) or explicitly changing their flags.


The only thing which affects the output of an int are the
different formatting parameters in ios_base: the flags, the fill
character and the width. (For some reason, C++ doesn't use the
precision field, although C does.) These flags can only be
manipulated by member functions. Manipulators call these member
functions.

By convention, inserters (operator<<) which use the width field
should reset it to 0, and change no other formatting parameters
(so fields like precision or the flags are sticky). This is
just a convention, however, and a rogue operator<< can change
things it's not supposed to. (That is, after all, how
manipulators work.)

> Neither printf nor any environmental variable should affect
> such formatting at all.


> My pick is that some call to some external library is directly
> affecting cout.


> You can check out cout's flags after those calls to find the
> offending call (adding a "cout << dec;" after that particular
> call, to solve the problem) otherwise you can explicitly put
> cout in decimal mode every time before outputting your ints -
> which should be the correct habit: never make assumptions on
> the current state of a shared resource, either check it or set
> it to the state you want it to be.


The "Internet principle" holds: be liberal in what you accept,
and conservative in what you send. Always restore the state
when you're finished, and never count on it being anything
specific.

--
James Kanze
 
Reply With Quote
 
Diwa
Guest
Posts: n/a
 
      10-02-2009
OP here. Thanks to all who responded.

Here are some facts. This program is multi-threaded (we know iostreams
are not thread safe).

On days when the cout screwup happens, right during program startup,
the logs show that while one thread is in the middle of printing addr
of a memory location using cout (hex by default), another thread
interrupts it with its own cout.

Maybe since the first thread was in the middle of printing addr in hex
format when it got interrupted, the hex flag remained set. Just
guessing.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      10-02-2009
On Oct 2, 1:58 pm, Diwa <(E-Mail Removed)> wrote:
> OP here. Thanks to all who responded.


> Here are some facts. This program is multi-threaded (we know
> iostreams are not thread safe).


> On days when the cout screwup happens, right during program
> startup, the logs show that while one thread is in the middle
> of printing addr of a memory location using cout (hex by
> default), another thread interrupts it with its own cout.


I know of no implementation which can handle this. You need
some sort of external synchronization for all of your output to
any given instance of an ostream. (You should not need
synchronization for output to different instances.)

> Maybe since the first thread was in the middle of printing
> addr in hex format when it got interrupted, the hex flag
> remained set. Just guessing.


Something like "std::cout << std::hex << someInt" resolves to:
operator<<( std::cout, std::hex ).operator<<( someInt )
There's no way the implementation can prevent a thread switch
between the two calls to operator<<; it's in user code. It's up
to you to ensure that the stream object is properly
synchronized.

--
James Kanze
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      10-02-2009
On 2 Ott, 14:58, Diwa <(E-Mail Removed)> wrote:
> OP here. Thanks to all who responded.
>
> Here are some facts. This program is multi-threaded (we know iostreams
> are not thread safe).
>
> On days when the cout screwup happens, right during program startup,
> the logs show that while one thread is in the middle of printing addr
> of a memory location using cout (hex by default), another thread
> interrupts it with its own cout.
>
> Maybe since the first thread was in the middle of printing addr in hex
> format when it got interrupted, the hex flag remained set. Just
> guessing.


You can completely walk around this issue by printing your ints to a
stringstream and then dumping its content to cout. Then you can
completely ignore the status of cout and build and keep an invariant
state of your output(s).

Hope that helps, I have no grip on threads, also, I suppose my
suggestion should involve some performance loss.

Have good time,
Francesco
--
Francesco S. Carta, hobbyist
http://fscode.altervista.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterator Question for map of ints to set of ints uclamathguy@gmail.com C++ 3 04-03-2005 03:26 AM
ints ints ints and ints Skybuck Flying C Programming 24 07-10-2004 04:48 AM
std::cout vs cout Pmb C++ 2 06-02-2004 03:27 PM
compile error with "cout << "Hexadecimal == 0x" << hex << m << endl;" Ensoul Chee C++ 7 09-09-2003 02:12 AM
man cout or info cout abi C++ 2 06-28-2003 06:42 PM



Advertisments