Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Any way to have ostream not auto-extend sign bit when printingaddresses?

Reply
Thread Tools

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

 
 
David T
Guest
Posts: n/a
 
      03-07-2009
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



 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      03-08-2009
On Mar 7, 4:36 pm, David T <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      03-08-2009
On Mar 7, 5:14 pm, Jeff Schwab <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-08-2009
* 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: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!
 
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
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
Strange compiler error w.r.t 'friend ostream& operator<< <>(ostream&,...)' abhay.burli@gmail.com C++ 2 02-26-2009 06:18 AM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments