Re: ptr conversions and values
- From: kuyper@xxxxxxxxxx
- Date: 6 Sep 2005 19:25:40 -0700
S.Tobias wrote:
> kuyper@xxxxxxxxxx wrote:
....
> > An address doesn't know what object type it is really pointing at.
> > However, a C pointer isn't just a memory address; it always has a
> > specific type it's pointing at, (possibly an incomplete one, and in the
> > case of void, it might be an incomplete type that can't be completed).
> > As such it only directly points at an object of it's pointed-at type.
>
> Well, no:
> void *v = malloc(sizeof(int));
> int *pi = v;
> *pi = 7;
> float *pf = v; // pf points to int object
No, it points at a location which has the right alignment to store a
float, and which could actually contain a float if
sizeof(float)<=sizeof(int). That location currently contains an int,
but pf doesn't point at that int, since it can't be used to retrieve
that int except by converting it into a pointer of a different type.
It's that pointer of a different type which points at the int object.
If it's a pointer to int, it points at the int as an int. If it's a
pointer to unsigned chare, it points to the int as an array of unsigned
char.
I'll grant you that pf and pi point at the same location in memory;
they just don't point at the same complete object (even if both object
types have the same size). At any one time, one of them points at an
object of the pointed-at type, and the other doesn't point at any valid
object.
....
> That was my point. I just haven't found any guarantees that in
> some conversion the location is turned to the _end_ of an object
> (or to the middle, or elsewhere); ...
In the general case, there is indeed no such guarantee.
> ... and the list was not all about
> conversions after all. I tried to give a few reasons why I thought
> the first byte of an object was distinguished by the language.
It is, in many contexts; but not in the sense of actually being pointed
at by pointer that don't have character type.
.
- Follow-Ups:
- Re: ptr conversions and values
- From: Wojtek Lerch
- Re: ptr conversions and values
- References:
- Re: ptr conversions and values
- From: S.Tobias
- Re: ptr conversions and values
- From: kuyper
- Re: ptr conversions and values
- From: S.Tobias
- Re: ptr conversions and values
- Prev by Date: Re: Secure C library
- Next by Date: Re: ptr conversions and values
- Previous by thread: Re: ptr conversions and values
- Next by thread: Re: ptr conversions and values
- Index(es):
Relevant Pages
|