Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Whats the use of %p

Reply
Thread Tools

Whats the use of %p

 
 
Keith Thompson
Guest
Posts: n/a
 
      02-26-2006
Jordan Abel <> writes:
> On 2006-02-26, CBFalconer <> wrote:
>> Keith Thompson wrote:

[...]
>>> Quoting the standard:
>>>
>>> The argument shall be a pointer to void. The value of the
>>> pointer is converted to a sequence of printing characters,
>>> in an implementation-defined manner.

>>
>> Is it even guaranteed to vary across different pointers? I imagine
>> an output of, say, "here lieth a soggy pointer" would meet the
>> standard. It might not be considered an especially high quality
>> implementation.

>
> I believe it's guaranteed that you can feed it to scanf %p and get a
> pointer that is the same as [compares equal to] the original.


Yes. C99 7.19.6.2:

Matches an implementation-defined set of sequences, which should
be the same as the set of sequences that may be produced by the %p
conversion of the fprintf function. The corresponding argument
shall be a pointer to a pointer to void. The input item is
converted to a pointer value in an implementation-defined
manner. If the input item is a value converted earlier during the
same program execution, the pointer that results shall compare
equal to that value; otherwise the behavior of the %p conversion
is undefined.

Which implies that the result of printf("%p", ptr) is unique for each
unique pointer value, so CBFalconer's suggestion elsethread:

] Is it even guaranteed to vary across different pointers? I imagine
] an output of, say, "here lieth a soggy pointer" would meet the
] standard. It might not be considered an especially high quality
] implementation.

would not be conforming.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      02-27-2006
In article <%NcMf.24996$>
MrG{DRGN} <> wrote:
>... What (if any) is/are the use(s) of %p in printf in a situation where
>the output isn't formatted as a number of some sort?


Others mentioned "segmentffset" style output (which one might
expect, and I believe did in fact get, on some 80x86 C compilers).
A more interesting example occurs on the IBM AS/400. I have not
used it but I have seen it described. In fact, google has
archived a message in which someone paraphrases the %p output
as:

SPP:0000 :1aefRMA_INTPTRMJK 070591 :10320:4:28

(search group comp.sys.ibm.as400.misc for "printf", "pointer", and
perhaps "%p").
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
 
 
 
websnarf@gmail.com
Guest
Posts: n/a
 
      02-27-2006
MrG{DRGN} wrote:
> "Keith Thompson" <kst-> wrote in message
> news:...
> > "MrG{DRGN}" <> writes:
> >> "Richard Bos" <> wrote in message
> >> news:...
> >>> "Spidey" <> wrote:
> >>>
> >>>> What kind of formating can be done with %p in printf
> >>>
> >>> None. You can use it to print a pointer value; how the output is
> >>> formatted is entirely up to the implementation.
> >>
> >> Richard by the above do you mean that the output of %p is not guaranteed
> >> to
> >> be in hexadecimal format?(as shown elsethread, and also the output on my
> >> with my compiler/computer)

> >
> > Correct; it's not even guaranteed to look anything like a number.
> >
> > Quoting the standard:
> >
> > The argument shall be a pointer to void. The value of the pointer
> > is converted to a sequence of printing characters, in an
> > implementation-defined manner.

>
>
> Thanks for the info Keith! Ok then my next questions at this point would be
> these. What (if any) is/are the use(s) of %p in printf in a situation where
> the output isn't formatted as a number of some sort?


On some systems, pointers are *NOT* numbers. In 16 bit DOS for
example, a pointer is a 16 bit segment and and 16 bit offset and may
look like: 0FDC:0110 for example.

> [...] Are you still able to
> determine the address by somehow deciphering the non-numerical sequence of
> printing characters?


According to the quoted standard, that is not guaranteed.

> [...] If not then is there any portable usefulness for %p in
> printf?


No ... C is not a portable language, and this is merely one example of
why its not. This is just meant for system-level debugging. By
knowing what the %p format does on your system, you may be able to
understand what its output means, and that's all you really have.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

 
Reply With Quote
 
websnarf@gmail.com
Guest
Posts: n/a
 
      02-27-2006
SM Ryan wrote:
> "Malcolm" <> wrote:
> # >
> # > What kind of formating can be done with %p in printf
> # >
> # There's never any point in printing out a pointer except to debug code.
> # Hence no reason to format nicely for the user's consumption.
>
> Creates unique strings for things like hash table keys.


How do you know that?

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

 
Reply With Quote
 
Ian Malone
Guest
Posts: n/a
 
      02-27-2006
Keith Thompson wrote:

>
> Yes. C99 7.19.6.2:
>
> Matches an implementation-defined set of sequences, which should
> be the same as the set of sequences that may be produced by the %p
> conversion of the fprintf function. The corresponding argument
> shall be a pointer to a pointer to void. The input item is
> converted to a pointer value in an implementation-defined
> manner. If the input item is a value converted earlier during the
> same program execution, the pointer that results shall compare
> equal to that value; otherwise the behavior of the %p conversion
> is undefined.
>
> Which implies that the result of printf("%p", ptr) is unique for each
> unique pointer value, so CBFalconer's suggestion elsethread:
>
> ] Is it even guaranteed to vary across different pointers? I imagine
> ] an output of, say, "here lieth a soggy pointer" would meet the
> ] standard. It might not be considered an especially high quality
> ] implementation.
>
> would not be conforming.
>


Although implementing C with a scout troop and using their house
addresses as the output of %p would. Has this been tried I wonder?

--
imalone
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      02-27-2006
schrieb:
> SM Ryan wrote:
>
>>"Malcolm" <> wrote:
>># >
>># > What kind of formating can be done with %p in printf
>># >
>># There's never any point in printing out a pointer except to debug code.
>># Hence no reason to format nicely for the user's consumption.
>>
>>Creates unique strings for things like hash table keys.

>
> How do you know that?


As
void *bar;

fprintf(fptr, "%p", (void *)foo);
rewind(fptr);
fscanf("%p", &bar);

(checks omitted) gives you bar with foo==bar (C99, 7.19.6.2),
you can infer uniqueness of the conversion result.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-27-2006
writes:
> MrG{DRGN} wrote:

[...]
>> [...] If not then is there any portable usefulness for %p in
>> printf?

>
> No ... C is not a portable language, and this is merely one example of
> why its not. This is just meant for system-level debugging. By
> knowing what the %p format does on your system, you may be able to
> understand what its output means, and that's all you really have.


Your statement, "C is not a portable language", is either meaningless
or false.

The question of whether C, as a language, is "portable" could mean any
of a number of things. If you measure portability by the number of
platforms on which it's been implemented, I suspect that C is more
portable than any other programming language. If you're talking about
portability of C programs, it's entirely possible to write C programs
that are portable to any conforming implementation.

Writing portable code in C is perhaps more work than writing portable
code in some other languages. Writing non-portable code in C is easy
to do deliberately, and perhaps too easy to do unintentionally.
Inferring from this that "C is not a portable language" is nonsense.

Or perhaps I've misunderstood your statement; can you clarify it?

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
P.J. Plauger
Guest
Posts: n/a
 
      02-27-2006
"Keith Thompson" <kst-> wrote in message
news:...

> writes:
>> MrG{DRGN} wrote:

> [...]
>>> [...] If not then is there any portable usefulness for %p in
>>> printf?

>>
>> No ... C is not a portable language, and this is merely one example of
>> why its not. This is just meant for system-level debugging. By
>> knowing what the %p format does on your system, you may be able to
>> understand what its output means, and that's all you really have.

>
> Your statement, "C is not a portable language", is either meaningless
> or false.
>
> The question of whether C, as a language, is "portable" could mean any
> of a number of things. If you measure portability by the number of
> platforms on which it's been implemented, I suspect that C is more
> portable than any other programming language. If you're talking about
> portability of C programs, it's entirely possible to write C programs
> that are portable to any conforming implementation.
>
> Writing portable code in C is perhaps more work than writing portable
> code in some other languages. Writing non-portable code in C is easy
> to do deliberately, and perhaps too easy to do unintentionally.
> Inferring from this that "C is not a portable language" is nonsense.
>
> Or perhaps I've misunderstood your statement; can you clarify it?


He's trolling -- I did a couple laps around the barn with him a
month or two ago on the same subject.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


 
Reply With Quote
 
websnarf@gmail.com
Guest
Posts: n/a
 
      02-28-2006
Keith Thompson wrote:
> writes:
> > MrG{DRGN} wrote:

> [...]
> >> [...] If not then is there any portable usefulness for %p in
> >> printf?

> >
> > No ... C is not a portable language, and this is merely one example of
> > why its not. This is just meant for system-level debugging. By
> > knowing what the %p format does on your system, you may be able to
> > understand what its output means, and that's all you really have.

>
> Your statement, "C is not a portable language", is either meaningless
> or false.


MrG{DRGN}, who claims to be a person new to C, asked the following
question:

> > [...] is there any portable usefulness for %p in printf?


Why do you think he's asking that? He has either some expectation, or
has read some propaganda somewhere suggesting that C is a portable
language. He sees this and realizes that its a blatant contradiction
of this notion. Being a new programmer to C, he has no stake in this.
He hasn't cherry picked some case to make C look bad. Its a
contradiction and that motivates him to ask the question.

And as you well know, there is an endless supply of these
"contradictions" for the C language. Your position, is to modify the
notion of portability, rather than simply accepting the mountain of
examples (each one by itself being sufficient) that basically tell you
that "C" is not, by itself, portable.

> The question of whether C, as a language, is "portable" could mean any
> of a number of things. If you measure portability by the number of
> platforms on which it's been implemented,


Mutating the definition ... the word you are looking for is
"availability".

> [...] I suspect that C is more portable than any other programming language.


No, by that measure assembly is more portable.

> [...] If you're talking about
> portability of C programs, it's entirely possible to write C programs
> that are portable to any conforming implementation.


Its possible to write Fortran, BASIC, and Pascal programs that are also
portable as well. Its also possible to write programs which are
portable to multiple programming languages too (look up polygots).

> Writing portable code in C is perhaps more work than writing portable
> code in some other languages. Writing non-portable code in C is easy
> to do deliberately, and perhaps too easy to do unintentionally.
> Inferring from this that "C is not a portable language" is nonsense.


Ok, so you are saying, by that reasoning, that Fortran, BASIC and
Pascal are similarly portable languages then?

> Or perhaps I've misunderstood your statement; can you clarify it?


Well, I think you just misunderstand the content of MrG{DRGN}'s
question. He's asking how can C be considered portable if %p formating
doesn't have portable semantics.

Why do you think stdint.h was added to the C99 standard? If C were a
portable language, why would anyone need such a file? The file should
basically be called "PortableIntegers.h" because that's what it is --
because the old C never hada portable integers. How is a language
supposed to be considered portable without portable integers?

For those that cannot let go of the notion that C is portable, you
simply mutate the meaning of portable and say that its ok for %p to be
different, and that doesn't affect portability. Its much like the
dogmatic religious who sit on the notions they believe and simply
mutate the world, science, logic, around them so that they can retain
their belief in the rightness of their religion.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-28-2006
writes:
> Keith Thompson wrote:
>> writes:
>> > MrG{DRGN} wrote:

>> [...]
>> >> [...] If not then is there any portable usefulness for %p in
>> >> printf?
>> >
>> > No ... C is not a portable language, and this is merely one example of
>> > why its not. This is just meant for system-level debugging. By
>> > knowing what the %p format does on your system, you may be able to
>> > understand what its output means, and that's all you really have.

>>
>> Your statement, "C is not a portable language", is either meaningless
>> or false.

>
> MrG{DRGN}, who claims to be a person new to C, asked the following
> question:
>
>> > [...] is there any portable usefulness for %p in printf?


He asked a very simple question about a single feature, and you
insisted on making an inflammatory statement about the language as a
whole.

> Why do you think he's asking that? He has either some expectation, or
> has read some propaganda somewhere suggesting that C is a portable
> language. He sees this and realizes that its a blatant contradiction
> of this notion. Being a new programmer to C, he has no stake in this.
> He hasn't cherry picked some case to make C look bad. Its a
> contradiction and that motivates him to ask the question.


Or maybe he just wants to know whether there's any portable usefulness
for %p in printf.

[more nonsense snipped]

--
Keith Thompson (The_Other_Keith) kst- <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.
 
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
whats the use of void pointers? sam C++ 4 06-26-2007 08:36 PM
whats use of ":" in java in the following code ? Madni Java 4 06-12-2006 01:18 PM
whats the best firewall to use Milanjot Computer Support 5 04-24-2006 06:18 PM
Divx-Xvid movie repair software whats a good program to use ? Digital Sheep DVD Video 3 04-13-2005 05:55 PM
whats the use of third argument to main( ) thesushant@rediffmail.com C Programming 6 01-11-2005 03:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57