malloc()/free() question

P

Peter Nilsson

Harald said:
However, there's no guarantee that two pointer values that compare
equal have identical representations.

True, but I don't print pointers to compare them directly. I print
them
in debugging logs. I simply want to see what they are. If I compare
them, I do so visually and with inherent knowledge of the system
at hand. Knowledge, I couldn't endow on a program.
Usually this won't matter, but if you expect to be able to compare the
representations of two pointers, it will.

I would convert the text representation back into a pointer before
comparing them, same way I would for integers or doubles etc...
[Not that I read pointer values from streams that often... well... at
all!]
Additionally, it has been decided that once the value becomes
indeterminate, the representation also becomes indeterminate.

But it can still be printed via unsigned char lvalues.
Quoting part of the response to DR #260:

"Values may have any bit-pattern that validly represents them and
the implementation is free to move between alternate
representations (for example, it may normalize pointers, floating-
point representations etc.). In the case of an indeterminate value
all bit-patterns are valid representations and the actual bit-pattern
may change without direct action of the program."

It's just covering the case of variable 'colouring' from what I've
seen,
though obviously the scope is potentially broader.
Unfortunately, this response fairly directly contradicts the standard
for reasons already explained in the DR, and as far as I am aware,
the text of the standard has not been updated to support the
response, so I'm not sure of its official status.

Doesn't matter. It can (and does) happen in practice anyway, so
programmers may as well take it as read.
Does this have improvements over %p other than the possibility
of printing trap representations?

Firstly, it prints the whole representation, not just 'value' bits
as many %p implementations do. Secondly, it's possible to print
to a precise width. Judicious use of CHAR_BIT means I can
make pointers print to a consistent size which is useful in hex
dumps with an address down the left hand side.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top