473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

outputting a NULL pointer to a stream


Hello!

I have a piece of code that needs to display a formatted
table of pointers using streams, with the pointers represented
as hex values. It looks more or less like this:

#include <iostream>
#include <iomanip>

using namespace std;

// ptr[] is an array of pointers

int main() {
for(unsigned int i=0;i<some_size;++i) {

cout << hex << setprecision(16) << setw(20)
<< (void*)ptr[i] << '\n';

}

}

What annoys me is that if ptr[i] is NULL, then it does
get output as ' 0' instead of
'0x0000000000000000' as I'd like it to print.

Can anyone suggest a solution to this, other than
checking for NULL manually? Casting to int is not an
option since pointers are 8-byte and ints are 4-byte
on my system.

thanks in advance,
- J.
Nov 22 '05 #1
13 2726
Jacek Dziedzic wrote:
Hello!

I have a piece of code that needs to display a formatted
table of pointers using streams, with the pointers represented
as hex values. It looks more or less like this:

#include <iostream>
#include <iomanip>

using namespace std;

// ptr[] is an array of pointers

int main() {
for(unsigned int i=0;i<some_size;++i) {

cout << hex << setprecision(16) << setw(20)
<< (void*)ptr[i] << '\n';

}

}

What annoys me is that if ptr[i] is NULL, then it does
get output as ' 0' instead of
'0x0000000000000000' as I'd like it to print.

Can anyone suggest a solution to this, other than
checking for NULL manually? Casting to int is not an
option since pointers are 8-byte and ints are 4-byte
on my system.

thanks in advance,
- J.


Use std::cout::fill( '0' ) or the equivalent I/O manipulator. I'll bet
a pointer to, say, address 0x100 also prints without the zeros (i.e., '
100'). BTW, you omitted the "0x" in your cout
statement.

M

Nov 22 '05 #2
mlimber wrote:

Use std::cout::fill( '0' ) or the equivalent I/O manipulator. I'll bet
a pointer to, say, address 0x100 also prints without the zeros (i.e., '
100'). BTW, you omitted the "0x" in your cout
statement.


You've lost your bet :). Since I've used std::hex, the
pointers print out as (for instance)

0x2000000002affc00
0x2000000007ccda34
0x2000000009418afb
0
0x2000000009418afb
0x2000000009418afb

etc.

without having to manually specifying "0x" and setting the fill-char
to "0". But not for NULL, alas.

- J.
Nov 22 '05 #3
"Jacek Dziedzic" <jacek@no_spam.tygrys.no_spam.net> wrote in message
news:80**************************@news.chello.pl.. .
mlimber wrote:

Use std::cout::fill( '0' ) or the equivalent I/O manipulator. I'll bet
a pointer to, say, address 0x100 also prints without the zeros (i.e., '
100'). BTW, you omitted the "0x" in your cout
statement.


You've lost your bet :). Since I've used std::hex, the
pointers print out as (for instance)

0x2000000002affc00
0x2000000007ccda34
0x2000000009418afb
0
0x2000000009418afb
0x2000000009418afb

etc.

without having to manually specifying "0x" and setting the fill-char
to "0". But not for NULL, alas.


You missed something. Don't you notice the first value is 2? 0x2....
that's why you're not missing any leading zeros, you dont' have any!

So he hasn't lost the bet yet.
Nov 22 '05 #4
Jim Langston wrote:
You missed something. Don't you notice the first value is 2? 0x2....
that's why you're not missing any leading zeros, you dont' have any!

So he hasn't lost the bet yet.


void *p=(void*)0x100;
void *q=(void*)main; // for instance
void *r=NULL;

cout << fixed << setfill('0');
cout << hex << setprecision(16) << setw(18) << p << '\n';
cout << hex << setprecision(16) << setw(18) << q << '\n';
cout << hex << setprecision(16) << setw(18) << r << '\n';

produces

00000000000000x100
0x4000000000001990
000000000000000000

while I'd like to have

0x0000000000000100
0x4000000000001990
0x0000000000000000

.... so while mliber was right about "a pointer to, say, address
0x100 also prints without the zeros" [without setfill('0')]

he missed the 'hex' issue. Therefore, alas, setfill() does not
help me, or I can't see it helping me.

What I can't understand is, why this special value of NULL
would not print with "0x" in front of it like all other
hex values (for pointers).

Will I *really* need to cast this pointer to two (or more)
int values and output them as hex, followed by "0x" which
I'll have to add manually?

- J.
Nov 22 '05 #5
Jacek Dziedzic wrote:
Jim Langston wrote:
You missed something. Don't you notice the first value is 2? 0x2....
that's why you're not missing any leading zeros, you dont' have any!

So he hasn't lost the bet yet.

void *p=(void*)0x100;
void *q=(void*)main; // for instance
void *r=NULL;

cout << fixed << setfill('0');
cout << hex << setprecision(16) << setw(18) << p << '\n';
cout << hex << setprecision(16) << setw(18) << q << '\n';
cout << hex << setprecision(16) << setw(18) << r << '\n';

produces

00000000000000x100
0x4000000000001990
000000000000000000

while I'd like to have

0x0000000000000100
0x4000000000001990
0x0000000000000000

... so while mliber was right about "a pointer to, say, address
0x100 also prints without the zeros" [without setfill('0')]

he missed the 'hex' issue. Therefore, alas, setfill() does not
help me, or I can't see it helping me.

What I can't understand is, why this special value of NULL
would not print with "0x" in front of it like all other
hex values (for pointers).

Will I *really* need to cast this pointer to two (or more)
int values and output them as hex, followed by "0x" which
I'll have to add manually?

- J.


You might find this helps

cout << hex << setprecision(16) << internal << setw(18) << p << '\n';

john
Nov 22 '05 #6
John Harrison wrote:
You might find this helps

cout << hex << setprecision(16) << internal << setw(18) << p << '\n';


It doesn't, unfortunately. AFAIK 'internal' is used if you
want to have the number aligned right and the sign to the left,
correct? Unfortunately "0x" does not want to behave like a
sign.

I browsed the whole section on manipulators in my Josuttis
book and it doesn't say a word on outputting pointers. Perhaps
the implementation is rather free as to the way it outputs
them?

I'm lost now and the prospect of outputting them converted
in sizeof(char) pieces looms.

thanks,
- J.
Nov 22 '05 #7
"Jacek Dziedzic" <jacek@no_spam.tygrys.no_spam.net> wrote in message
news:8b***************************@news.chello.pl. ..
John Harrison wrote:
You might find this helps

cout << hex << setprecision(16) << internal << setw(18) << p << '\n';


It doesn't, unfortunately. AFAIK 'internal' is used if you
want to have the number aligned right and the sign to the left,
correct? Unfortunately "0x" does not want to behave like a
sign.

I browsed the whole section on manipulators in my Josuttis
book and it doesn't say a word on outputting pointers. Perhaps
the implementation is rather free as to the way it outputs
them?

I'm lost now and the prospect of outputting them converted
in sizeof(char) pieces looms.


Only thing I could think of would be to somehow print them without the 0x
prefix and add it manually. Don't think there is a flag for that though :/
Nov 22 '05 #8
Jim Langston wrote:
Only thing I could think of would be to somehow print them without the 0x
prefix and add it manually. Don't think there is a flag for that though :/


I gave up and resorted to:

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

string ptr_to_str(const void* const ptr) {

// represent the pointer as unsigned char[]
unsigned char ptr_representation[sizeof(void*)];
memcpy(ptr_representation,&ptr,sizeof(void*));

string result;
stringstream ss;
ss << "0x" << hex << setfill('0');

// iterate over the array in a little-endian manner
for(int i=sizeof(void*)-1; i>=0; --i) {
unsigned int k = ptr_representation[i];
ss << setw(2) << k;
}
ss >> result;
return result;
}

int main() {
void *p=NULL;
void *q=(void*)&main; // for instance
void *r=(void*)0x123; // for instance

cout << ptr_to_str(p) << "\n";
cout << ptr_to_str(q) << "\n";
cout << ptr_to_str(r) << "\n";
}

and this produces

0x0000000000000000
0x4000000000009e60
0x0000000000000123

just as desired, only for little-endian platforms, however.

- J.
Nov 22 '05 #9
In article <8b***************************@news.chello.pl>,
Jacek Dziedzic <jacek@no_spam.tygrys.no_spam.net> wrote:
the implementation is rather free as to the way it outputs
[pointers]?


99.99% sure it is implementation-defined.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #10
Jacek Dziedzic wrote:
Jim Langston wrote:
You missed something. Don't you notice the first value is 2? 0x2....
that's why you're not missing any leading zeros, you dont' have any!

So he hasn't lost the bet yet.


void *p=(void*)0x100;
void *q=(void*)main; // for instance
void *r=NULL;

cout << fixed << setfill('0');
cout << hex << setprecision(16) << setw(18) << p << '\n';
cout << hex << setprecision(16) << setw(18) << q << '\n';
cout << hex << setprecision(16) << setw(18) << r << '\n';

produces

00000000000000x100
0x4000000000001990
000000000000000000

while I'd like to have

0x0000000000000100
0x4000000000001990
0x0000000000000000

... so while mliber was right about "a pointer to, say, address
0x100 also prints without the zeros" [without setfill('0')]

he missed the 'hex' issue. Therefore, alas, setfill() does not
help me, or I can't see it helping me.

[snip]

It looks like you have the ios::showbase flag enabled. Try turning that
off, specifying the base indicator yourself, and using fill:

void* p = 0;
cout << hex << noshowbase << "0x" << setfill( '0' )
<< setw( 18 ) << p << endl;

Cheers! --M

Nov 22 '05 #11
mlimber wrote:
Jacek Dziedzic wrote:
Jim Langston wrote:
You missed something. Don't you notice the first value is 2? 0x2....
that's why you're not missing any leading zeros, you dont' have any!

So he hasn't lost the bet yet.


void *p=(void*)0x100;
void *q=(void*)main; // for instance
void *r=NULL;

cout << fixed << setfill('0');
cout << hex << setprecision(16) << setw(18) << p << '\n';
cout << hex << setprecision(16) << setw(18) << q << '\n';
cout << hex << setprecision(16) << setw(18) << r << '\n';

produces

00000000000000x100
0x4000000000001990
000000000000000000

while I'd like to have

0x0000000000000100
0x4000000000001990
0x0000000000000000

... so while mliber was right about "a pointer to, say, address
0x100 also prints without the zeros" [without setfill('0')]

he missed the 'hex' issue. Therefore, alas, setfill() does not
help me, or I can't see it helping me.


[snip]

It looks like you have the ios::showbase flag enabled. Try turning that
off, specifying the base indicator yourself, and using fill:

void* p = 0;
cout << hex << noshowbase << "0x" << setfill( '0' )
<< setw( 18 ) << p << endl;


Ah yes, works like a charm! I didn't know (no)showbase
worked for pointers too. It's not a common manipulator also,
I guess. Your method is much cleaner, although the 18
requires some tuning depending on sizeof(void*).

thanks a lot,
- J.
Nov 22 '05 #12
Jacek Dziedzic wrote:


void *p=(void*)0x100;
void *q=(void*)main; // for instance
void *r=NULL;

cout << fixed << setfill('0');
cout << hex << setprecision(16) << setw(18) << p << '\n';
cout << hex << setprecision(16) << setw(18) << q << '\n';
cout << hex << setprecision(16) << setw(18) << r << '\n';

produces

00000000000000x100
0x4000000000001990
000000000000000000
That's odd.

Will I *really* need to cast this pointer to two (or more)
int values and output them as hex, followed by "0x" which
I'll have to add manually?


Seems so.

If possible, I would also contact the compiler vendor and tell them
about this. It doesn't seem to be right and to be a bug in the
I/O library.

--
Karl Heinz Buchegger
kb******@gascad.at
Nov 22 '05 #13
Karl Heinz Buchegger wrote:
Jacek Dziedzic wrote:

void *p=(void*)0x100;
void *q=(void*)main; // for instance
void *r=NULL;

cout << fixed << setfill('0');
cout << hex << setprecision(16) << setw(18) << p << '\n';
cout << hex << setprecision(16) << setw(18) << q << '\n';
cout << hex << setprecision(16) << setw(18) << r << '\n';

produces

00000000000000x100
0x4000000000001990
000000000000000000

That's odd.

Will I *really* need to cast this pointer to two (or more)
int values and output them as hex, followed by "0x" which
I'll have to add manually?

Seems so.

If possible, I would also contact the compiler vendor and tell them
about this. It doesn't seem to be right and to be a bug in the
I/O library.


And what *is* wrong specifically? I checked it with the g++
compiler and got the same output.

And about contacting the compiler vendor... I contacted them
a month ago using their premier support to complain about a
rather serious bug in a well-known implementation of Linear Algebra
PACKage they sell. They have reproduced the bug but seemingly do
not care to fix it, at least not within a month. I guess won't
hassle them with petty things then :).

- J.
Nov 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: nish.parikh | last post by:
Hi, I am using std::cout to print a char pointer that is NULL. Subsequent calls to std::cout dont print anything. Is this the expected behavior? example: #include <iostream> int main( int...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
35
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
39
by: ferrad | last post by:
I am trying to open a file for appending. ie. if the file exists, open it for appending, if not, create it. I am getting back a NULL pointer, and do not know why. Here is my code: FILE...
3
by: snow.carriers | last post by:
Let me first state that I'm using Borland Turbo C++, it's relatively old so the new string methods won't work. Anyways, first I'm trying to collect a line of a string (with numbers, letters,...
3
by: N. Spiker | last post by:
I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from...
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
17
by: Matt | last post by:
Hello. I'm having a very strange problem that I would like ot check with you guys. Basically whenever I insert the following line into my programme to output the arguments being passed to the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.