Any way to have ostream not auto-extend sign bit when printingaddresses?

Discussion in 'C++' started by David T, Mar 7, 2009.

  1. David T

    David T Guest

    Hi,

    When I use an ostream to print a address/pointer, it will
    automatically extend the sign bit.

    For example, suppose a pointer has a value of 0xaf46d00c. If I print
    it, using
    the "<<" operator, I get this:
    0xffffffffaf46d00c
    I would like it to print as 0xaf46d00c.

    The code is generally like this:
    void* myptr;
    .....
    clog << myptr << endl;

    Any ideas on how I can use something like a global setf() flag to make
    it not
    extend the sign bit when printing all addresses?

    Thanks,
    David
    David T, Mar 7, 2009
    #1
    1. Advertising

  2. David T

    James Kanze Guest

    On Mar 7, 4:36 pm, David T <> wrote:

    > When I use an ostream to print a address/pointer, it will
    > automatically extend the sign bit.


    > For example, suppose a pointer has a value of 0xaf46d00c. If
    > I print it, using
    > the "<<" operator, I get this:
    > 0xffffffffaf46d00c
    > I would like it to print as 0xaf46d00c.


    > The code is generally like this:
    > void* myptr;
    > .....
    > clog << myptr << endl;


    > Any ideas on how I can use something like a global setf() flag
    > to make it not extend the sign bit when printing all
    > addresses?


    The output format of a pointer is implementation defined. None
    of the formatting arguments are required to affect it (although
    from a QoI point of view, I would expect at least width to be
    taken into account). Sign extending a 16 bit value seems a bit
    strange, but it's the implementation's decision.

    If you want to control the output format, you really only have
    one choice: reinterpret_cast the pointer to the correct integral
    type (probably unsigned). And of course, even then, you might
    have to "interpret" the output somewhat, e.g. if the
    architecture doesn't have linear addressing.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 8, 2009
    #2
    1. Advertising

  3. David T

    James Kanze Guest

    On Mar 7, 5:14 pm, Jeff Schwab <> wrote:
    > David T wrote:


    > > When I use an ostream to print a address/pointer, it will
    > > automatically extend the sign bit.


    > > For example, suppose a pointer has a value of 0xaf46d00c.
    > > If I print it, using the "<<" operator, I get this:
    > > 0xffffffffaf46d00c
    > > I would like it to print as 0xaf46d00c.


    > > The code is generally like this:
    > > void* myptr;
    > > .....
    > > clog << myptr << endl;


    > > Any ideas on how I can use something like a global setf()
    > > flag to make it not extend the sign bit when printing all
    > > addresses?


    > This is a tough one for me to test at the moment, because GCC
    > actually provides overloads for pointer types.


    The standard requires overloads for void*, char* and I think
    unsigned char* and signed char*. His problem is that he doesn't
    like the format the implementation does for void*.

    > Here's a proposed solution that depends on pointers fitting in
    > std::size_t, but I'm not sure whether that's guaranteed by the
    > standard.


    Of course it's not. I've done a lot of programming on systems
    where sizeof( size_t ) == 2, but sizeof( void* ) == 4. More
    generally, the standard doesn't even guarantee that sizeof(
    void* ) == sizeof( int* ), and there are machines being sold
    today where this is not the case.

    In modern C, in the next release of C++, and in any really good
    implementation of C++ today, there will be a uintptr_t, defined
    in <stdint.h> (or as std::uintptr_t in <cstdint>, of course, in
    the future C++).

    I'm still a little curious about his platform. What platform
    has 16 bit pointers, but outputs them as 32 bit values?

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 8, 2009
    #3
  4. * James Kanze:
    >
    > If you want to control the output format [for a pointer], you really
    > only have one choice: reinterpret_cast the pointer to the correct
    > integral type (probably unsigned). And of course, even then, you
    > might have to "interpret" the output somewhat, e.g. if the
    > architecture doesn't have linear addressing.


    Well, an alternative C++ way is to output the pointer value, static_cast'ed to
    void const*, to a std::eek:stringstream, then deal with string value. But I'd
    prefer to use sprintf. :) Of course, I'm pretty sure your preference is for the
    iostream solution, and if so there is merit in that position: by simply banning
    all that C i/o stuff one removes a whole big breeding ground for bugs -- but
    then, at least for my personal preference, convenience & efficiency counts more.

    Cheers,

    - Alf

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
    Alf P. Steinbach, Mar 8, 2009
    #4
    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. Jimmy
    Replies:
    1
    Views:
    930
    Cowboy \(Gregory A. Beamer\)
    Nov 21, 2006
  2. Mufasa
    Replies:
    7
    Views:
    360
    Mufasa
    Jun 11, 2007
  3. linkswanted
    Replies:
    1
    Views:
    896
  4. Replies:
    2
    Views:
    1,897
  5. dare ruby
    Replies:
    4
    Views:
    104
    Heesob Park
    Mar 20, 2010
Loading...

Share This Page