473,549 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
'0x000000000000 0000' 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 2745
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
'0x000000000000 0000' 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)

0x2000000002aff c00
0x2000000007ccd a34
0x2000000009418 afb
0
0x2000000009418 afb
0x2000000009418 afb

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)

0x2000000002aff c00
0x2000000007ccd a34
0x2000000009418 afb
0
0x2000000009418 afb
0x2000000009418 afb

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

00000000000000x 100
0x4000000000001 990
000000000000000 000

while I'd like to have

0x0000000000000 100
0x4000000000001 990
0x0000000000000 000

.... 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

00000000000000x 100
0x4000000000001 990
000000000000000 000

while I'd like to have

0x0000000000000 100
0x4000000000001 990
0x0000000000000 000

... 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.chell o.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(cons t void* const ptr) {

// represent the pointer as unsigned char[]
unsigned char ptr_representat ion[sizeof(void*)];
memcpy(ptr_repr esentation,&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_representat ion[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

0x0000000000000 000
0x4000000000009 e60
0x0000000000000 123

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

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

Similar topics

6
3095
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 argc, char *argv ) { char *q = 0; std::cout << q << "\n";
102
5935
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" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ?...
35
9908
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 the program. Since fgets() doesn't return the number of characters read it is pretty tough to handle the embedded nulls once they are in the buffer....
5
24677
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 s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
39
23747
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 *pFile; char *cFileName; cFileName=argv; pFile = fopen(cFileName,"a+"); cFileName has a valid filename ('apm.dbg'), and I have full write
3
2176
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, dashes) into each variable. For just numbers, it's relatively easy: ifstream fout("s1.in"); for (a=0; a<17; a++) { fout >> data; cout << data <<...
3
6442
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 the socket is missing the last character (line feed). When the same text is sent without the urgent flag set, all of the characters are read. ...
4
10493
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 doesn't seem to write to the screen in the way I would expect. The output is:
17
3355
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 programme: printf("\nCommand line arguement %d: %s.", i , argv ); The porgramme outputs 3 of the command line arguements, then gives a...
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7450
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.