casting pointers

R

rihad

Hi, I've this question: suppose we have two differently typed pointers:

struct foo *foo;
char **array;

Becase one can always legally cast any pointer to (void *), and becase (void *)
is assignable to any pointer type, is it ever necessary to cast when assigning
one pointer type to another? I.e. since

foo = (void *) array;

is legal (or is it?), isn't the (void *) there just line noise?

TIA
 
T

The Real OS/2 Guy

Hi, I've this question: suppose we have two differently typed pointers:

struct foo *foo;
char **array;

Becase one can always legally cast any pointer to (void *), and becase (void *)
is assignable to any pointer type, is it ever necessary to cast when assigning
one pointer type to another? I.e. since

foo = (void *) array;

No: foo = ((void*) ((void*) ((void*) ((void*) ((void*) array)))));

or better another thousend of casts would be neccessary.
is legal (or is it?), isn't the (void *) there just line noise?

No a char array is NOT a struct foo. You can cast float* to unsigned
char - but that makes no more sense than casing a pointer of one type
to another type in general.

And casting a pointer to a type to a pointer void to cast this pointer
to another type makes it no more legal. Because ever when you casts a
pointer to void you're only allowed to cast that pointer _back_ to the
type it was before. A pointer to void is nothing than a container that
hides the original type until you needs that type back.
 
M

Micah Cowan

rihad said:
Hi, I've this question: suppose we have two differently typed pointers:

struct foo *foo;
char **array;

Becase one can always legally cast any pointer to (void *), and becase (void *)
is assignable to any pointer type, is it ever necessary to cast when assigning
one pointer type to another? I.e. since

foo = (void *) array;

I'm not following your logic, which appears to be
non-sequitur. It is *because* you can legally cast any pointer to
(void*), which can then be implicitly converted to the
appropriate type, that the above is correct. It is due to the
fact that the same is *not* guaranteed for other pointer types
that the (void*) isn't just noise.

Casting exists for a very good reason: to protect you from
inadvertant mistakes. The standard could have just decided to
always convert (when possible) to the type of the left-hand
operator to =. I'm damn glad it didn't.

-Micah
 
R

Robert W Hand

struct foo *foo;
char **array;
foo = (void *) array;

is legal (or is it?), isn't the (void *) there just line noise?

This construction approaches silliness. The issue is whether the
conversion of array to foo is reasonable. The language allows
conversion between pointers to different objects, but there are issues
of alignment, issues of potential undefined behavior, issues of syntax
violations, and issues of semantics. I would inspect each pointer
conversion carefully to make sure it is what is needed and to make
sure it is sensible.

Best wishes,

Bob
 
C

CBFalconer

Micah said:
.... snip ...

I'm not following your logic, which appears to be
non-sequitur. It is *because* you can legally cast any pointer to
(void*), which can then be implicitly converted to the
appropriate type, that the above is correct. It is due to the
fact that the same is *not* guaranteed for other pointer types
that the (void*) isn't just noise.

No. You can cast any pointer type to void*, and then you can cast
that back to the original pointer type. Casting that void* to a
type other than the original is specifically NOT guaranteed.

This means that you can use void* as a mechanism to pass pointers
through code that has no idea what it really is or does. Sooner
or later it arrives somewhere where its actual original type is
known, and it can then be restored. This sort of thing lets you
supply compare routines to qsort, which couldn't care less what
the items are, it only needs to know which of two are larger, and
be able to trade pointers to them. However the caller must know
what the items really are, and how to compare them.
 
R

Richard Heathfield

CBFalconer wrote:

You can cast any pointer type to void*, and then you can cast
that back to the original pointer type.

Why bother? You can do the assignment without casting:

SOME_OBJECT_TYPE_OR_OTHER *p = CreateSomeObjectType();
void *v = p;
p = v;

No need for casting.
Casting that void* to a
type other than the original is specifically NOT guaranteed.

True, but neither is not casting it. :)
This means that you can use void* as a mechanism to pass pointers
through code that has no idea what it really is or does. Sooner
or later it arrives somewhere where its actual original type is
known, and it can then be restored.

Precisely. (And darned useful it is, too.)
 
P

Peter Nilsson

CBFalconer said:
No. You can cast any pointer type to void*, and then you can cast
that back to the original pointer type. Casting that void* to a
type other than the original is specifically NOT guaranteed.

The conversion itself is guaranteed explicitly in 6.3.2.3p1, dereferencing
the resulting pointer is a different story.
 
M

Micah Cowan

CBFalconer said:
No. You can cast any pointer type to void*, and then you can cast
that back to the original pointer type. Casting that void* to a
type other than the original is specifically NOT guaranteed.

Yeah, but I was speaking of syntax. It may invoke UB, but it is
guaranteed to *run* (even if the definition of "run" for this
platform is to screechingly halt). It *will* be implicitly
converted (from void*, that is). What happens after that was not
a part of what I was saying: I was speaking strictly in terms of
what guarantees can be made at "compile time" (in standardspeke,
I'm talking about what does and doesn't violate constraints).

-Micah
 
M

Micah Cowan

Peter Nilsson said:
The conversion itself is guaranteed explicitly in 6.3.2.3p1, dereferencing
the resulting pointer is a different story.

Erm, well, sorta. The conversion is definitely guaranteed, but
even without dereferencing it can invoke UB simply due to the
fact of conversion (provided the alignment is wrong). Still,
convert it shall, and as I was talking about the legality of his
statement and not the advisability or definedness, I think I'm
alright.

-Micah
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top