Connecting Tech Pros Worldwide Help | Site Map

pointer to void property

aegis
Guest
 
Posts: n/a
#1: Nov 15 '05
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?


--
aegis

Nerox
Guest
 
Posts: n/a
#2: Nov 15 '05

re: pointer to void property


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;

Irrwahn Grausewitz
Guest
 
Posts: n/a
#3: Nov 15 '05

re: pointer to void property


"aegis" <aegis@mad.scientist.com> wrote:[color=blue]
>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?[/color]

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.
--
Irrwahn Grausewitz (irrwahn35@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Irrwahn Grausewitz
Guest
 
Posts: n/a
#4: Nov 15 '05

re: pointer to void property


"Nerox" <nerox0@gmail.com> wrote:

Please preserve some context when posting replies. Context restored:
[color=blue]
> "aegis" <aegis@mad.scientist.com> wrote:[color=green]
> >Given the following:
> >
> >int a = 10;
> >[color=darkred]
> > >int *p;[/color]
> >void *p1;
> >unsigned char *p2;
> >
> >p = &a;
> >
> >p1 = p;
> >
> >p2 = p1;
> >
> >is a guarantee made that I can access the object representation
> >via 'p2'?[/color][/color]
[color=blue]
>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;[/color]

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.
--
Irrwahn Grausewitz (irrwahn35@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Emmanuel Delahaye
Guest
 
Posts: n/a
#5: Nov 15 '05

re: pointer to void property


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

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?"


Jack Klein
Guest
 
Posts: n/a
#6: Nov 15 '05

re: pointer to void property


On Sun, 11 Sep 2005 12:11:00 +0200, Irrwahn Grausewitz
<irrwahn35@freenet.de> wrote in comp.lang.c:
[color=blue]
> "aegis" <aegis@mad.scientist.com> wrote:[color=green]
> >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?[/color]
>
> 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.[/color]

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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
aegis
Guest
 
Posts: n/a
#7: Nov 15 '05

re: pointer to void property



Irrwahn Grausewitz wrote:[color=blue]
> "aegis" <aegis@mad.scientist.com> wrote:[color=green]
> >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?[/color]
>
> 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.[/color]

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.

--
aegis

pete
Guest
 
Posts: n/a
#8: Nov 15 '05

re: pointer to void property


aegis wrote:[color=blue]
>
> Irrwahn Grausewitz wrote:[/color]
[color=blue][color=green]
> > 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.[/color][/color]
[color=blue]
> 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[/color]

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

--
pete
Irrwahn Grausewitz
Guest
 
Posts: n/a
#9: Nov 15 '05

re: pointer to void property


Jack Klein <jackklein@spamcop.net> wrote:[color=blue]
>On Sun, 11 Sep 2005 12:11:00 +0200, Irrwahn Grausewitz[/color]
<snip>[color=blue][color=green]
>> 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.[/color]
>
>Or even better, just assigning and allowing the automatic conversion
>to happen. No casting required.[/color]

I meant to say conversion, not casting. You're of course right.
--
Irrwahn Grausewitz (irrwahn35@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Irrwahn Grausewitz
Guest
 
Posts: n/a
#10: Nov 15 '05

re: pointer to void property


"aegis" <aegis@mad.scientist.com> wrote:[color=blue]
>
>Irrwahn Grausewitz wrote:[/color]
<snip>[color=blue][color=green]
>> But casting from any[/color][/color]
^^^^^^^^^^^^^^^^^^^^^^^[color=blue][color=green]
>> pointer-to-sometype to pointer-to-void is safe, as is casting from any[/color][/color]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^[color=blue][color=green]
>> pointer-to-void to a pointer-to-charactertype.[/color][/color]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

s/casting/conversion/
[color=blue]
>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[/color]

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.
--
Irrwahn Grausewitz (irrwahn35@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Old Wolf
Guest
 
Posts: n/a
#11: Nov 15 '05

re: pointer to void property


Irrwahn Grausewitz wrote:[color=blue]
>
> 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...[/color]

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.

Mabden
Guest
 
Posts: n/a
#12: Nov 15 '05

re: pointer to void property


"Old Wolf" <oldwolf@inspire.net.nz> wrote in message
news:1126566243.859723.271250@g47g2000cwa.googlegr oups.com...[color=blue]
> Irrwahn Grausewitz wrote:[color=green]
> >
> > 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...[/color]
>
> 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.
>[/color]

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).

--
Mabden


Irrwahn Grausewitz
Guest
 
Posts: n/a
#13: Nov 15 '05

re: pointer to void property


"Old Wolf" <oldwolf@inspire.net.nz> wrote:[color=blue]
>Irrwahn Grausewitz wrote:[color=green]
>>
>> 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...[/color]
>
>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?[/color]
<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
--
Irrwahn Grausewitz (irrwahn35@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Irrwahn Grausewitz
Guest
 
Posts: n/a
#14: Nov 15 '05

re: pointer to void property


"Mabden" <mabden@sbc_global.net> wrote:[color=blue]
>"Old Wolf" <oldwolf@inspire.net.nz> wrote in message
>news:1126566243.859723.271250@g47g2000cwa.googleg roups.com...[/color]
<snip>[color=blue][color=green]
>> 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?[/color][/color]
<snip>[color=blue]
>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).[/color]

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
--
Irrwahn Grausewitz (irrwahn35@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Christopher Benson-Manica
Guest
 
Posts: n/a
#15: Nov 15 '05

re: pointer to void property


Irrwahn Grausewitz <irrwahn35@freenet.de> wrote:
[color=blue]
> Of course. A pointer-to-charactertype can be used to access the[/color]
^^^^^^^^^^^^^^^^^^^^^^^^

Only if the type in question is unsigned.
[color=blue]
> individual bytes of any object that occupies the memory location
> said pointer happens to point to (Ref: C99 6.3.2.3p7).[/color]

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Peter Nilsson
Guest
 
Posts: n/a
#16: Nov 15 '05

re: pointer to void property


Christopher Benson-Manica wrote:[color=blue]
> Irrwahn Grausewitz <irrwahn35@freenet.de> wrote:
>[color=green]
> > Of course. A pointer-to-charactertype can be used to access the[/color]
> ^^^^^^^^^^^^^^^^^^^^^^^^
>
> Only if the type in question is unsigned.[/color]

Did you read 6.3.2.3p7?
[color=blue][color=green]
> > individual bytes of any object that occupies the memory location
> > said pointer happens to point to (Ref: C99 6.3.2.3p7).[/color][/color]

...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.

--
Peter

Irrwahn Grausewitz
Guest
 
Posts: n/a
#17: Nov 15 '05

re: pointer to void property


"Peter Nilsson" <airia@acay.com.au> wrote:[color=blue]
>Christopher Benson-Manica wrote:[color=green]
>> Irrwahn Grausewitz <irrwahn35@freenet.de> wrote:
>>[color=darkred]
>> > Of course. A pointer-to-charactertype can be used to access the[/color]
>> ^^^^^^^^^^^^^^^^^^^^^^^^
>>
>> Only if the type in question is unsigned.[/color]
>
>Did you read 6.3.2.3p7?
>[color=green][color=darkred]
>> > individual bytes of any object that occupies the memory location
>> > said pointer happens to point to (Ref: C99 6.3.2.3p7).[/color][/color]
>
> ...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.[/color]

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

Best regards
--
Irrwahn Grausewitz (irrwahn35@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Closed Thread