cout printing ints in hexadecimal instead of decimal

Discussion in 'C++' started by Diwa, Oct 1, 2009.

  1. Diwa

    Diwa Guest

    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?
     
    Diwa, Oct 1, 2009
    #1
    1. Advertising

  2. Diwa <> 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__
     
    Pascal J. Bourguignon, Oct 1, 2009
    #2
    1. Advertising

  3. On 1 Ott, 17:26, Diwa <> 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
     
    Francesco S. Carta, Oct 1, 2009
    #3
  4. Diwa

    James Kanze Guest

    On Oct 1, 7:04 pm, "Francesco S. Carta" <> wrote:
    > On 1 Ott, 17:26, Diwa <> 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
     
    James Kanze, Oct 2, 2009
    #4
  5. Diwa

    Diwa Guest

    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.
     
    Diwa, Oct 2, 2009
    #5
  6. Diwa

    James Kanze Guest

    On Oct 2, 1:58 pm, Diwa <> 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
     
    James Kanze, Oct 2, 2009
    #6
  7. On 2 Ott, 14:58, Diwa <> 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
     
    Francesco S. Carta, Oct 2, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,652
  2. Ensoul Chee
    Replies:
    7
    Views:
    686
    Ensoul Chee
    Sep 9, 2003
  3. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,409
    Leor Zolman
    Jun 2, 2004
  4. Replies:
    3
    Views:
    564
    Mark P
    Apr 3, 2005
  5. Skybuck Flying

    ints ints ints and ints

    Skybuck Flying, Jul 8, 2004, in forum: C Programming
    Replies:
    24
    Views:
    835
    Jack Klein
    Jul 10, 2004
Loading...

Share This Page