cast to 'void *'

  • Thread starter Borked Pseudo Mailed
  • Start date
B

Borked Pseudo Mailed

The cast appears necessary in that case, since you're using
an (int *).

If you were using a (char *), whether the cast is necessary
has been debated. When I pointed out a subclause and footnote
about the interchangeability of (void *) and (char *) - a fact
that I knew wasn't unknown to people on this newsgroup, since
it's been discussed here before - there was talk of the standard
being useless on the point because it had made its intention known
in a non-normative footnote and not the actual body of the standard.
It's a pedantry thing.

For what it's worth, I recommend always using the (void *) cast
for %p, even for (char *), just to be sure.

(I'm assuming you know why those assignments are a bad idea.)

Hope that helps.

Yours,
Han from China
 
M

Mark

Borked Pseudo Mailed wrote on Fri, 5 Dec 2008 01:00:50 -0700 (MST):

BPM> For what it's worth, I recommend always using the (void *) cast
BPM> for %p, even for (char *), just to be sure.

Thanks for the explanation.

BPM> (I'm assuming you know why those assignments are a bad idea.)

Would you please clarify what assignments you were referring to ?

-- Mark
 
J

James Kuyper

Mark said:
Borked Pseudo Mailed wrote on Fri, 5 Dec 2008 01:00:50 -0700 (MST):

BPM> For what it's worth, I recommend always using the (void *) cast
BPM> for %p, even for (char *), just to be sure.

Thanks for the explanation.

BPM> (I'm assuming you know why those assignments are a bad idea.)

Would you please clarify what assignments you were referring to ?

The code you provided contains exactly two assignments:
> p = x;
> x = p;

Both are bad ideas because, before these assignment statements, 'x' is
uninitialized, and therefore has an indeterminate value. One of the
possibilities for an object with a indeterminate value is that it might
contain a trap representation. Any attempt to read the value of an
object containing a trap representation causes your entire program to
have undefined behavior, which means that in principle it could do anything.

In practice, there are real computers for which some pointer
representations represent invalid machine addresses, and any attempt to
even load such a representation into an address register will cause your
program to be aborted. Naturally, pointer assignment is performed by
loading from memory into an address register, and then writing the value
from that register to a different location in memory. Pretty nasty.
Definitely something you should be trying to avoid doing.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top