473,395 Members | 2,079 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,395 software developers and data experts.

Pointer to integral conversion

Hi,

I have a small interrogation concerning pointer to integer conversion:
how is it possible to safely and portably print a memory address ? From
what I understand (according to FAQ 4.14), conversion from pointer to
integral is never never guaranteed. But (correct me if I'm wrong) some
kind of conversion _is_ necessary in order to print the numeric value.

I have taken a look to the gnu libc printf implementation, and it
appears that the void * is being casted into the largest available
integer type. Do I have a standard and portable way to know about this
"largest integer type" ?

Thank you in advance.

--
icecrime
Nov 24 '06 #1
6 2618
icecrime wrote:
Hi,

I have a small interrogation concerning pointer to integer conversion:
how is it possible to safely and portably print a memory address ?
printf("%p", (void *) p);
From
what I understand (according to FAQ 4.14), conversion from pointer to
integral is never never guaranteed. But (correct me if I'm wrong) some
kind of conversion _is_ necessary in order to print the numeric value.
Yes. This conversion may not be available in a standard way, though,
and even if it is, there's no guarantee that %p prints a numeric value.
I have taken a look to the gnu libc printf implementation, and it
appears that the void * is being casted into the largest available
integer type.
That's possible because glibc is not meant to be ported to platforms
where pointers are larger than the largest integer type.
Do I have a standard and portable way to know about this
"largest integer type" ?
In C90, there's (signed/unsigned) long. In C99, there's intmax_t
(signed) and uintmax_t (unsigned), which you get by including
<stdint.h>. These types may be too small to store pointer values. More
appropriate would be intptr_t and uintptr_t, optionally defined in the
same header; when they're not available, intmax_t and uintmax_t
probably won't work either.

Nov 24 '06 #2
Hi,
>
I have a small interrogation concerning pointer to integer conversion:
how is it possible to safely and portably print a memory address ? From
what I understand (according to FAQ 4.14), conversion from pointer to
integral is never never guaranteed. But (correct me if I'm wrong) some
kind of conversion _is_ necessary in order to print the numeric value.
Use the "%p" conversion specifier. The libc shall take care of the
internal representation.

HTH,
Loic.

Nov 24 '06 #3
icecrime <ic******@gmail.comwrote:
I have a small interrogation concerning pointer to integer conversion:
how is it possible to safely and portably print a memory address ?
You use printf("%p", (void *)your_pointer).
From what I understand (according to FAQ 4.14), conversion from pointer to
integral is never never guaranteed. But (correct me if I'm wrong) some
kind of conversion _is_ necessary in order to print the numeric value.
True, but there's the problem: there is not necessarily a meaningful
numeric value for pointers. Not all computers have a flat memory space.
I have taken a look to the gnu libc printf implementation, and it
appears that the void * is being casted into the largest available
integer type. Do I have a standard and portable way to know about this
"largest integer type" ?
Yes and no. In C89, the largest (unsigned, which is what you want)
integer type is unsigned long. This may not be large enough to represent
all pointer values, though.

In C99, the situation is even more complex. The largest unsigned integer
is uintmax_t, which is required to exist in <stdint.h>; since this must
be at least as large as an unsigned long long, it is more likely to be
large enough for a pointer.
There is also an even better choice in the same header: uintptr_t. A
void pointer can be converted to this type and back without changing
value. The problem with this type is that it's optional. You can find
out if your implementation has it by checking if UINTPTR_MAX is #defined
in <stdint.h>.
For both these types, you can find macros needed to print them safely in
<inttypes.h>; if you #include that header, it also #includes <stdint.h>,
so you don't need to do that as well.

In the end, though, I'd just go with casting the pointer to (void *) and
using "%p". That's portable everywhere.

Richard
Nov 24 '06 #4
icecrime wrote:
Hi,

I have a small interrogation concerning pointer to integer conversion:
how is it possible to safely and portably print a memory address ? From
what I understand (according to FAQ 4.14), conversion from pointer to
integral is never never guaranteed. But (correct me if I'm wrong) some
kind of conversion _is_ necessary in order to print the numeric value.
You can print the value of a `void*' pointer (but not necessarily
any other kind of pointer) with the "%p" specifier. Whether this is
portable or not depends on what you mean by "portable:" the action of
printing the pointer is portable, but the characters printed are not:
both their identities and their format are implementation-defined.
I have taken a look to the gnu libc printf implementation, and it
appears that the void * is being casted into the largest available
integer type. Do I have a standard and portable way to know about this
"largest integer type" ?
For C90 `[unsigned] long' is the largest integer type, but it is
not guaranteed to be large enough to hold all pointer values. In
C99 you have `[u]intmax_t', but `[u]intptr_t' would be a better choice
for the purpose at hand. Even in C99 there are no guarantees about
how the conversion works, except that it is reversible: If you convert
a `void*' to a `[u]intptr_t' and back again, the result compares equal
to the original. Nothing is specified about the form of the integer
in the middle, or how it is transformed from and to the pointer.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 24 '06 #5
"Harald van Dijk" <tr*****@gmail.comwrites:
icecrime wrote:
>Hi,

I have a small interrogation concerning pointer to integer conversion:
how is it possible to safely and portably print a memory address ?

printf("%p", (void *) p);
Yes, but only for object pointers. For function pointers, the
conversion to void* may or may not work; it does on many
implementations, but it's not guaranteed. The only general way to
print a function pointer is to look at the bytes making up its
representation; even then, the meaning of those bytes is entirely
implementation-specific.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 24 '06 #6
Keith Thompson wrote:
"Harald van Dijk" <tr*****@gmail.comwrites:
>icecrime wrote:
>>Hi,

I have a small interrogation concerning pointer to integer conversion:
how is it possible to safely and portably print a memory address ?
printf("%p", (void *) p);

Yes, but only for object pointers. For function pointers, the
conversion to void* may or may not work; it does on many
implementations, but it's not guaranteed. The only general way to
print a function pointer is to look at the bytes making up its
representation; even then, the meaning of those bytes is entirely
implementation-specific.
Additionally, a function pointer need not point to object memory, or indeed
to any sort of memory at all. If it's valid and non-null it'll point to a
particular function, but that's all we can portably say.

S.
Nov 24 '06 #7

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

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
11
by: Chris Dams | last post by:
Dear all, I found out that the program #include<vector> using namespace std; int main() { vector<int*> v(2,0);
21
by: Chen Shusheng | last post by:
Hello, I have a small piece of code that I want to directly assign a segment of memory to a pointer. But complier tell me wrong. Pls you help.Codes below: ------------------ int * p;...
9
by: junky_fellow | last post by:
Hi, To print the pointer using printf(), we convert it to (void *) . printf("%p",(void *)ptr); My question is how printf() determine which type of pointer is passed to it and prints its value...
11
by: jois.de.vivre | last post by:
I am interfacing with a third party API (written in C, if that matters) that has an "event handler" function with the following definition: void event_handler(int event_code, unsigned long...
1
by: Neelesh Bodas | last post by:
Can somebody please explain the fine difference between these two clauses of the standard: 4.5 (Integral promotions) 4. An rvalue of type bool can be converted to an rvalue of type int, with...
17
by: Ivan K. | last post by:
I am looking at some legacy code, which begins by allocating a double matrix with the dmatrix() function from NRC as follows: double **A, **augin, **augout, **aa; A = dmatrix(1,...
25
by: Jrdman | last post by:
is it possible to use a pointer to see the data stored in a given address in the memory ? if no what's the appropriate way to do that? thanks.
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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...
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
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...

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.