473,471 Members | 2,040 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sprintf problem

Hello, I want to display the address a pointer points to in hexadecimal form
in the same way my debugger displays it.
Say I have P* ptr; where P is some type.

I tried:
std::sprintf(message, "%#0x, (unsigned int)ptr);

But that gives me: 0xFFFF for example, when I want 0x0000FFFF, i.e., I dont
want to drop any leading zeros. I know this is just a small cosmetic
problem, but I still would like to know how to accomplish this.

Thanks for any replies!

/ WP
Jul 22 '05 #1
10 1923

"William Payne" <mi**************@student.liu.se> wrote in message
news:ci**********@news.island.liu.se...
Hello, I want to display the address a pointer points to in hexadecimal form in the same way my debugger displays it.
Say I have P* ptr; where P is some type.

I tried:
std::sprintf(message, "%#0x, (unsigned int)ptr);

But that gives me: 0xFFFF for example, when I want 0x0000FFFF, i.e., I dont want to drop any leading zeros. I know this is just a small cosmetic
problem, but I still would like to know how to accomplish this.

Thanks for any replies!


#include <stdio.h>

#define char_count (sizeof (unsigned int) * 2)

int main()
{
unsigned int i = 0;
int *ptr = &i;
char message[2 * sizeof *ptr] = {0};

sprintf(message, "%0*x", 2 * sizeof ptr, (unsigned int)ptr);

puts(message);
return 0;
}

Notes:

The result of casting a pointer to unsigned int is implementation defined.

The format specifier indicating a pointer is %p.

The format of the text produced with the %p specifier is implementation
defined.

The size of type 'unsigned int' is not necessarily four bytes (but must be
at least two bytes).

The size of a byte is not necessarily eight bits (but must be at
least eight bits).

IOW: the above code is not portable.

-Mike
Jul 22 '05 #2

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Eh**************@newsread1.news.pas.earthlink .net...
#include <stdio.h>

#define char_count (sizeof (unsigned int) * 2)


This macro is not used. Just a 'mental note'
I forgot to throw away. :-)

-Mike
Jul 22 '05 #3

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:am***************@newsread1.news.pas.earthlin k.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Eh**************@newsread1.news.pas.earthlink .net...


Using C++ IOStreams:

#include <iomanip>
#include <ios>
#include <iostream>
#include <sstream>

int main()
{
int i = 0;
int *ptr = &i;
std::ostringstream oss;

oss << std::hex
<< std::setfill('0')
<< std::setw(2* sizeof *ptr)
<< ptr
<< '\n';

std::cout << oss.str().c_str() << '\n';
return 0;
}
-Mike
Jul 22 '05 #4

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:zs**************@newsread1.news.pas.earthlink .net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:am***************@newsread1.news.pas.earthlin k.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Eh**************@newsread1.news.pas.earthlink .net...


Using C++ IOStreams:

#include <iomanip>
#include <ios>
#include <iostream>
#include <sstream>

int main()
{
int i = 0;
int *ptr = &i;
std::ostringstream oss;

oss << std::hex
<< std::setfill('0')
<< std::setw(2* sizeof *ptr)
<< ptr
<< '\n';

std::cout << oss.str().c_str() << '\n';
return 0;
}
-Mike


Thanks Mike! This last example is totally portable? I need portability
across MSVC 7.1 and GCC 3.4.2, but I prefer using completely standard
methods that does not rely on undefined behaviour.

/ WP
Jul 22 '05 #5

"William Payne" <mi**************@student.liu.se> wrote in message
news:ci**********@news.island.liu.se...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:zs**************@newsread1.news.pas.earthlink .net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:am***************@newsread1.news.pas.earthlin k.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Eh**************@newsread1.news.pas.earthlink .net...
Using C++ IOStreams:

#include <iomanip>
#include <ios>
#include <iostream>
#include <sstream>

int main()
{
int i = 0;
int *ptr = &i;
std::ostringstream oss;

oss << std::hex
<< std::setfill('0')
<< std::setw(2* sizeof *ptr)
<< ptr
<< '\n';

std::cout << oss.str().c_str() << '\n';
return 0;
}
-Mike


Thanks Mike! This last example is totally portable?


No. The same issues exits about conversion from integer
to pointer types, and about type size assumptions.
I need portability
across MSVC 7.1 and GCC 3.4.2,
The above isn't *universally* portable, but should be portable
among most PC operating systems.
but I prefer using completely standard
methods
There's no standard way to convert an integer to a pointer.
You can further generalize by factoring 'CHAR_BIT' into the
calculation of the output field width. But you need only
be concerned about this if you intend to port to systems
where 'CHAR_BIT' != 8.
that does not rely on undefined behaviour.


Note that undefined behavior and implementation defined behavior
are distinct specifications. The above code has implementation
defined behavior, not undefined.

-Mike
Jul 22 '05 #6
On Mon, 13 Sep 2004 23:44:36 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c++:

Notes:

The result of casting a pointer to unsigned int is implementation defined.
If and only if unsigned int is large enough to hold the pointer. See
paragraph 4 of 5.2.10 reinterpret_cast.
The format specifier indicating a pointer is %p.

The format of the text produced with the %p specifier is implementation
defined.

The size of type 'unsigned int' is not necessarily four bytes (but must be
at least two bytes).
No, it must not.
The size of a byte is not necessarily eight bits (but must be at
least eight bits).
Taking your last two statements above, you should be able to see why I
objected to the first one. I'm working on a 16-bit DSP right now
where CHAR_BIT is 16 and sizeof(unsigned int) is 1. It has a
reasonably conforming free-standing C++ implementation.

I have also used a 32-bit DSP where all of the integer types, char
through long, all had 32 bits and sizeof == 1. C only though, it
didn't have a C++ compiler.
IOW: the above code is not portable.

-Mike


The code isn't even necessarily defined. On 16-bit platforms with 32
bit pointers (MS-DOS, TI2812, Philips XA, for example), the behavior
is undefined. Likewise on a lot of the newer 64-bit Windows/*nix
implementations, where ints have 32 bits and pointers have 64.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #7
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:or********************************@4ax.com...
On Mon, 13 Sep 2004 23:44:36 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c++:

Notes:

The result of casting a pointer to unsigned int is implementation defined.

If and only if unsigned int is large enough to hold the pointer. See
paragraph 4 of 5.2.10 reinterpret_cast.
The format specifier indicating a pointer is %p.

The format of the text produced with the %p specifier is implementation
defined.

The size of type 'unsigned int' is not necessarily four bytes (but must
be at least two bytes).


No, it must not.


Right. Should have said "at least sixteen bits."
The size of a byte is not necessarily eight bits (but must be at
least eight bits).
Taking your last two statements above, you should be able to see why I
objected to the first one.


Yes. :-)
I'm working on a 16-bit DSP right now
where CHAR_BIT is 16 and sizeof(unsigned int) is 1. It has a
reasonably conforming free-standing C++ implementation.

I have also used a 32-bit DSP where all of the integer types, char
through long, all had 32 bits and sizeof == 1. C only though, it
didn't have a C++ compiler.
IOW: the above code is not portable.

-Mike


The code isn't even necessarily defined. On 16-bit platforms with 32
bit pointers (MS-DOS, TI2812, Philips XA, for example), the behavior
is undefined.


Which specific expressions are the trouble (i.e. undefined
rather than imp-defined)?

-Mike
Jul 22 '05 #8
On Tue, 14 Sep 2004 04:08:57 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c++:
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:or********************************@4ax.com...
On Mon, 13 Sep 2004 23:44:36 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c++:

Notes:

The result of casting a pointer to unsigned int is implementation defined.

If and only if unsigned int is large enough to hold the pointer. See
paragraph 4 of 5.2.10 reinterpret_cast.
The format specifier indicating a pointer is %p.

The format of the text produced with the %p specifier is implementation
defined.

The size of type 'unsigned int' is not necessarily four bytes (but must

be at least two bytes).


No, it must not.


Right. Should have said "at least sixteen bits."
The size of a byte is not necessarily eight bits (but must be at
least eight bits).


Taking your last two statements above, you should be able to see why I
objected to the first one.


Yes. :-)
I'm working on a 16-bit DSP right now
where CHAR_BIT is 16 and sizeof(unsigned int) is 1. It has a
reasonably conforming free-standing C++ implementation.

I have also used a 32-bit DSP where all of the integer types, char
through long, all had 32 bits and sizeof == 1. C only though, it
didn't have a C++ compiler.
IOW: the above code is not portable.

-Mike


The code isn't even necessarily defined. On 16-bit platforms with 32
bit pointers (MS-DOS, TI2812, Philips XA, for example), the behavior
is undefined.


Which specific expressions are the trouble (i.e. undefined
rather than imp-defined)?


The C++ standard says (5.2.10 Reinterpret cast, p4)

"A pointer can be explicitly converted to any integral type large
enough to hold it. The mapping function is implementation defined
[Note: it is intended to be unsurprising to those who know the
addressing structure of the underlying machine. ]"

This is the old undefined behavior because one or more cases are
specifically defined and the case under discussion is not one of them.
It is undefined by lack of definition. The fact that a pointer may be
explicitly converted by reinterpret_cast (or C cast) to any integer
type enough to hold it implicitly states that it may not be converted
to an integer type that is not wide enough to hold it.

In this case, I like the wording of the C standard better, it is more
explicit (C99 6.3.2.3 Pointers p6):

"Any pointer type may be converted to an integer type. Except as
previously specified, the result is implementation-defined. If the
result cannot be represented in the integer type, the behavior is
undefined. The result need not be in the range of values of any
integer type."

C99 provides for (optional) typedefs for intptr_t and uintptr_t, which
an implementation may define in <stdint.h> (almost certain to become
<cstdint> some day) if such types exist that can hold any pointer
type.

So on a CHAR_BIT 8 implementation:

double d = 50000.0;
double *dp = & d;
unsigned char uc;

uc = d; /* range exceeded, undefined behavior */
uc = dp; /* since pointers must be wider than 8 bits, ub */

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #9
"William Payne" <mi**************@student.liu.se> wrote in message news:<ci**********@news.island.liu.se>...
Hello, I want to display the address a pointer points to in hexadecimal form
in the same way my debugger displays it.
Say I have P* ptr; where P is some type.

I tried:
std::sprintf(message, "%#0x, (unsigned int)ptr);

But that gives me: 0xFFFF for example, when I want 0x0000FFFF, i.e., I dont
want to drop any leading zeros. I know this is just a small cosmetic
problem, but I still would like to know how to accomplish this.

Thanks for any replies!

/ WP


Assuming there are 8 octets for an address:-
std::sprintf(message, "0x%08x", (unsigned int)ptr);
should serve your purpose.

Vinod
Jul 22 '05 #10
Jack Klein <ja*******@spamcop.net> wrote in message news:<or********************************@4ax.com>. ..
On Mon, 13 Sep 2004 23:44:36 GMT, "Mike Wahler" [...]

I have also used a 32-bit DSP where all of the integer types, char
through long, all had 32 bits and sizeof == 1. C only though, it
didn't have a C++ compiler.
IOW: the above code is not portable.

-Mike


The code isn't even necessarily defined. On 16-bit platforms with 32
bit pointers (MS-DOS, TI2812, Philips XA, for example), the behavior
is undefined. Likewise on a lot of the newer 64-bit Windows/*nix
implementations, where ints have 32 bits and pointers have 64.


It's interesting working with the 2812 and trying to maintain
portability with - for our purposes - a power pc.

ifdef 2812
// 16 bit character
else
//
etc
Jul 22 '05 #11

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

Similar topics

2
by: Huey | last post by:
Hi All, I saved hashed values of hex into buf, then buf becomes unreadable by the means of calling puts(buf), printf("%s") and so on. Then, I need to convert the hex in buf into a char buffer...
13
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution...
2
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
12
by: babak | last post by:
Hi everyone I want to format a string (using sprintf) and put it in a messagebox but however I try to do it, it doesn't seem to work. Here is an example sample of what i try to do: char msg;...
10
by: Saurabh | last post by:
Hi all, I am working on RedHat Linux GCC 3.0. I am trying to convert a long to string through sprintf. but i m getting segmantation fault. I tried snprintf also but no avail.here is the piece...
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
12
by: Henryk | last post by:
Hey there, I have some problems with the following code snippet on a Virtex-4 PowerPC with a GCC based compiler char chData; sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ???...
11
by: karlc | last post by:
I encountered the problem as follows: #include <stdio.h> char* fun(char* b) { char* sTmp; static char d; bzero(d, BUFSIZ); sprintf(d, "%s:%s", "Test", b);
15
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.