Discussion: example rquired
Afficher un message
Vieux 21/10/2007, 08h25   #3
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: example rquired


"siju" <aarklon@gmail.com> wrote in message
> 1) in C language _converting_ a pointer of type 'T*' to
> 'void*' type and back is defined and has its uses.
>
> can anyone give examples especially on uses???
>

Look up qsort(). It uses void *s, and also shows why void*s are necessary.
>
> 2) However, trying to _reinterpret_ a pointer of type 'T*' as a
> 'void*' pointer (using the above technique) leads to undefined
> results.
>
> explanation on un defined results ??
>

This is a subtle one.

int *ptr;
void *vptr;

/* bad */
memcpy(&ptr, &vptr, sizeof(void *));
/* good */
ptr = vptr;

The reason is that void*s might carry about extra baggage. For instance some
machines have a 32-byte architecture, but implent 8 bit chars on top of it.
So char *s have an extra two bits to indicate offset. void *s obvious need
these extra two bits, because they can be converted to char *s and back.
So sizeof(int *) might not equal sizeof(void *).


--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

  Réponse avec citation
 
Page generated in 0,05168 seconds with 9 queries