pointer to void property

A

aegis

Given the following:

int a = 10;

int *p;
void *p1;
unsigned char *p2;

p = &a;

p1 = p;

p2 = p1;

is a guarantee made that I can access the object representation
via 'p2'?

Will the value assigned between different pointer to object types
through use of a generic pointer preserve this value?

I know it is guaranteed to be preserved if it is assigned from a
pointer to an object type to a void pointer and back again to said
pointer to object type. But will the value assigned between different
pointer to object types through use of a generic pointer, preserve this
value?
 
N

Nerox

The pointer value will be the same, but you must dereference it in the
correct way to get the expected result.
If you perform:
*p2, you will get the first byte of the a variable read as a uchar.
That's usefull when you want to see the internal representation of
individuals bytes.

Also remember to cast from void to a type in order to avoid warnings:
p2 = (unsigned char *) pl;
 
I

Irrwahn Grausewitz

aegis said:
Given the following:

int a = 10;

int *p;
void *p1;
unsigned char *p2;

p = &a;

p1 = p;

p2 = p1;

is a guarantee made that I can access the object representation
via 'p2'?

Will the value assigned between different pointer to object types
through use of a generic pointer preserve this value?

I know it is guaranteed to be preserved if it is assigned from a
pointer to an object type to a void pointer and back again to said
pointer to object type. But will the value assigned between different
pointer to object types through use of a generic pointer, preserve this
value?

As long as you stick with a character pointer, yes. Consider:

ISO/IEC 9899:1999 (E)

6.2.5p26
A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type.

6.3.2.3p1
A pointer to void may be converted to or from a pointer to any
incomplete or object type. [...]

6.3.2.3p7
A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned for the pointed-to type, the
behavior is undefined. Otherwise, when converted back again, the
result shall compare equal to the original pointer. When a pointer
to an object is converted to a pointer to a character type, the
result points to the lowest addressed byte of the object. Successive
increments of the result, up to the size of the object, yield
pointers to the remaining bytes of the object.

Note that (AFAICT) it's not guaranteed to work with, e.g., a
pointer-to-int and a pointer-to-double. But casting from any
pointer-to-sometype to pointer-to-void is safe, as is casting from any
pointer-to-void to a pointer-to-charactertype.

Best regards.
pointer.
 
I

Irrwahn Grausewitz

Please preserve some context when posting replies. Context restored:
The pointer value will be the same, but you must dereference it in the
correct way to get the expected result.
If you perform:
*p2, you will get the first byte of the a variable read as a uchar.
That's usefull when you want to see the internal representation of
individuals bytes.

Also remember to cast from void to a type in order to avoid warnings:
p2 = (unsigned char *) pl;

Uck, no, don't. Implicit conversion to and from void is perfectly
valid, as opposed to conversions between pointers to different types
other than void. After all, that's one of the reasons why generic
pointers were introduced in C, IIRC.

Best regards.
 
E

Emmanuel Delahaye

Nerox wrote on 11/09/05 :
Also remember to cast from void to a type in order to avoid warnings:
p2 = (unsigned char *) pl;

Assuming p2 has the type 'void *' and as long as you are using a
C-compiler, there is no need for that.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 
J

Jack Klein

aegis said:
Given the following:

int a = 10;

int *p;
void *p1;
unsigned char *p2;

p = &a;

p1 = p;

p2 = p1;

is a guarantee made that I can access the object representation
via 'p2'?

Will the value assigned between different pointer to object types
through use of a generic pointer preserve this value?

I know it is guaranteed to be preserved if it is assigned from a
pointer to an object type to a void pointer and back again to said
pointer to object type. But will the value assigned between different
pointer to object types through use of a generic pointer, preserve this
value?

As long as you stick with a character pointer, yes. Consider:

ISO/IEC 9899:1999 (E)

6.2.5p26
A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type.

6.3.2.3p1
A pointer to void may be converted to or from a pointer to any
incomplete or object type. [...]

6.3.2.3p7
A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned for the pointed-to type, the
behavior is undefined. Otherwise, when converted back again, the
result shall compare equal to the original pointer. When a pointer
to an object is converted to a pointer to a character type, the
result points to the lowest addressed byte of the object. Successive
increments of the result, up to the size of the object, yield
pointers to the remaining bytes of the object.

Note that (AFAICT) it's not guaranteed to work with, e.g., a
pointer-to-int and a pointer-to-double. But casting from any
pointer-to-sometype to pointer-to-void is safe, as is casting from any
pointer-to-void to a pointer-to-charactertype.

Or even better, just assigning and allowing the automatic conversion
to happen. No casting required.
 
A

aegis

Irrwahn said:
aegis said:
Given the following:

int a = 10;

int *p;
void *p1;
unsigned char *p2;

p = &a;

p1 = p;

p2 = p1;

is a guarantee made that I can access the object representation
via 'p2'?

Will the value assigned between different pointer to object types
through use of a generic pointer preserve this value?

I know it is guaranteed to be preserved if it is assigned from a
pointer to an object type to a void pointer and back again to said
pointer to object type. But will the value assigned between different
pointer to object types through use of a generic pointer, preserve this
value?

As long as you stick with a character pointer, yes. Consider:

ISO/IEC 9899:1999 (E)

6.2.5p26
A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type.

6.3.2.3p1
A pointer to void may be converted to or from a pointer to any
incomplete or object type. [...]

6.3.2.3p7
A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned for the pointed-to type, the
behavior is undefined. Otherwise, when converted back again, the
result shall compare equal to the original pointer. When a pointer
to an object is converted to a pointer to a character type, the
result points to the lowest addressed byte of the object. Successive
increments of the result, up to the size of the object, yield
pointers to the remaining bytes of the object.

Note that (AFAICT) it's not guaranteed to work with, e.g., a
pointer-to-int and a pointer-to-double. But casting from any
pointer-to-sometype to pointer-to-void is safe, as is casting from any
pointer-to-void to a pointer-to-charactertype.

I wasn't asking about pointer-to-int to pointer-to-double
nor about pointer-to-sometype to pointer-to-void but specifically about
pointer-to-sometype to pointer-to-void to
pointer-to-[signed|unsigned]char


With something involving pointer-to-int to pointer-to-void to
pointer-to-double, the result from what I can see, may not be correctly
aligned.
 
P

pete

aegis said:
Irrwahn Grausewitz wrote:
I wasn't asking about pointer-to-int to pointer-to-double
nor about pointer-to-sometype to pointer-to-void
but specifically about
pointer-to-sometype to pointer-to-void to
pointer-to-[signed|unsigned]char

Pointer to an object to pointer-to-[signed|unsigned]char,
is defined.
 
I

Irrwahn Grausewitz

Jack Klein said:
Or even better, just assigning and allowing the automatic conversion
to happen. No casting required.

I meant to say conversion, not casting. You're of course right.
 
I

Irrwahn Grausewitz

aegis said:
Irrwahn Grausewitz wrote:
But casting from any ^^^^^^^^^^^^^^^^^^^^^^^
pointer-to-sometype to pointer-to-void is safe, as is casting from any ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pointer-to-void to a pointer-to-charactertype.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

s/casting/conversion/

I wasn't asking about pointer-to-int to pointer-to-double
nor about pointer-to-sometype to pointer-to-void but specifically about
pointer-to-sometype to pointer-to-void to
pointer-to-[signed|unsigned]char

If it's safe to convert a p-to-sometype to p-to-void, AND it's safe
to convert a p-to-void to p-to-char, THEN it's safe to convert a
p-to-int to p-to-void to p-to-char.

What a typing mess...

Best regards.
 
O

Old Wolf

Irrwahn said:
If it's safe to convert a p-to-sometype to p-to-void, AND it's safe
to convert a p-to-void to p-to-char, THEN it's safe to convert a
p-to-int to p-to-void to p-to-char.

What a typing mess...

The OP's original question was (rephrased):

Can this p-to-char be used to access the representation of the
"sometype" that p-to-sometype was pointing to?

which doesn't seem to have been answered yet on this thread
(everyone was distracted by the casting issue). I'm not sure
of the answer personally.
 
M

Mabden

Old Wolf said:
The OP's original question was (rephrased):

Can this p-to-char be used to access the representation of the
"sometype" that p-to-sometype was pointing to?

which doesn't seem to have been answered yet on this thread
(everyone was distracted by the casting issue). I'm not sure
of the answer personally.

Well, even if you do so, you would have to account for (ie: guess) byte
alignments that might change between compiler versions, or CPU changes
(32bit vs 64bit currently), or OS changes (versions), or OS changes
(endian issues), or OS changes (devices like PDAs), or OS changes (you
get the idea).
 
I

Irrwahn Grausewitz

Old Wolf said:
The OP's original question was (rephrased):

Can this p-to-char be used to access the representation of the
"sometype" that p-to-sometype was pointing to?
<snip>

Of course. A pointer-to-charactertype can be used to access the
individual bytes of any object that occupies the memory location
said pointer happens to point to (Ref: C99 6.3.2.3p7).

IIRC OP's question was, if this is still valid when the p-to-char
was not converted directly from the pointer to the original object,
but "went through" a pointer-to-void. It is (Ref: C99 6.2.5p26,
6.3.2.3p1, 6.3.2.3p7).

Aside: in some sense, a pointer-to-void is just a pointer-to-char
in disguise: it shall, as the authors of the standard put it, have
the same representation and alignment requirements.

Best regards
 
I

Irrwahn Grausewitz

Well, even if you do so, you would have to account for (ie: guess) byte
alignments that might change between compiler versions, or CPU changes
(32bit vs 64bit currently), or OS changes (versions), or OS changes
(endian issues), or OS changes (devices like PDAs), or OS changes (you
get the idea).

If a C programmer messes around with the internal representation of
objects, he knows he's left the realm of portable C. Well, at least
he /should/ know. ;-)

Best regards
 
C

Christopher Benson-Manica

Irrwahn Grausewitz said:
Of course. A pointer-to-charactertype can be used to access the
^^^^^^^^^^^^^^^^^^^^^^^^

Only if the type in question is unsigned.
 
P

Peter Nilsson

Christopher said:
^^^^^^^^^^^^^^^^^^^^^^^^

Only if the type in question is unsigned.

Did you read 6.3.2.3p7?

...When a pointer to an object is converted to a pointer to a
character type, the result points to the lowest addressed byte of
the object. Successive increments of the result, up to the size
of the object, yield pointers to the remaining bytes of the object.

Of course, only unsigned chars are guaranteed to be able to
differentiate
all byte values.
 
I

Irrwahn Grausewitz

Peter Nilsson said:
Did you read 6.3.2.3p7?


...When a pointer to an object is converted to a pointer to a
character type, the result points to the lowest addressed byte of
the object. Successive increments of the result, up to the size
of the object, yield pointers to the remaining bytes of the object.

Of course, only unsigned chars are guaranteed to be able to
differentiate
all byte values.

I guess I used the word "access" in a sloppy fashion. Sorry for
causing confusion.

Best regards
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top