Re: How do you cast an integer to a pointer?
- From: "Wade Ward" <zaxfuuq@xxxxxxxxxxx>
- Date: Wed, 17 Oct 2007 02:19:49 -0700
"James Van Buskirk" <not_valid@xxxxxxxxxxx> wrote in message
news:ltGdnVzewbrSTo7anZ2dnUVZ_i2dnZ2d@xxxxxxxxxxxxxx
"Craig Powers" <enigma@xxxxxxxxxx> wrote in messageOvvero, per esempio, se
news:4713d044$0$63178$a726171b@xxxxxxxxxxxxxxxxxx
glen herrmannsfeldt wrote:
If sizeof(int) >= sizeof(void*)
you might be able to do the cast, but the use of the value of the
int is non-portable.
Whether that's an issue or not depends on your purpose in casting in the
first place. One usage I've made of casting into an int (in C++, rather
than C, but the issues are the same) was to store the value in an array
of other characteristics which were int. Then, all I wanted to do was
cast to and then back from an int. (I could have and probably should
have stored an array of POD structs instead, and foregone the
typecasting, I don't remember why I did it the way I did it.)
I believe that GH intentionally indroduces disinformation as it's
simply impossible for anyone to make posts as stupid as he does.
If you examine my original code you will see that pointers (and
handles elsewhere) are cast to INTEGER(C_INTPTR_T), which does
not have the problems that casting to INTEGER(C_INT) would. It's
just a waste of time for me to try to correct this flood of dis-
information when he can simply pile another flood on top of my
corrections, so I usually don't bother to even read his posts.
That should not lead you to believe that he has ever made a
followup to one of my posts that doesn't need correcting, just
that I have concluded that it's mostly futile anyhow.
void* pippo;
long pluto;
Puoi assegnare pluto a pippo, ma con un recast di pluto:
pippo=(void*)pluto;
"A pointer may be converted to an integral type large enough toOvvero, puoi assegnare il valore di un pointer ad una variabile
hold it; the required size is implementation-dependent. The
mapping function is also implementation dependent."
pluto=(long)pippo;
ma la variabile deve potere contenere il valore del pointer: se il
pointer e' un 32bit, occore
usare un tipo da 32 bit almeno... questo dipende dall' architettura del
processore.
Su un processore a 16bit, con 16bit di indirizzamento, un pointer e' un
16bit e basta un int.
Su processore a 32bit con 32bit di indirizzo ci vuole un tipo da 32bit
(di solito ma non
necessariamente, un long: basterebbe un int ma su alcuni 32bit, gli int
possono essere a 16bit..)
"An object of integral type may be explicitly converted to aVedi sopra; Puoi assegnare pluto a pippo, ma con un recast e pluto deve
pointer. The mapping always carries a sufficiently wide integer
converted from a pointer back to the same pointer, but is
otherwise implementation-dependent."
avere la stessa dimensione
fisica di pippo (se pippo e' 32bit, pluto deve essere almeno da 32bit,
salvo casi particolari ;-) )
pippo=(void*)pluto;
Può qualcuno che conosca questo soggetto commentare il suddetto?
A questo uno potrebbe chiedersi il perche' di tutte queste cose....
In realta' lo scambio interi/pointers ha solo alcune applicazioni
abbastanza particolari e non dovrebbe essere abusato.
Quella che mi viene in mente e' la gestione di hardware, con mappe
di indirizzi espresse in valori interi che poi vengono assegnate
a puntatori...
Ad ogni modo, queste operazioni sono molto legare al tipo di architettura
della CPU, alla dimensione dei tipi e quindi *poco* portabili, per cui.....
My Italian's a little weak, but I like Glen in every language.
--
wade ward
"Nicht verzagen, Bruder Grinde fragen."
.
- References:
- How do you cast an integer to a pointer?
- From: James Van Buskirk
- Re: How do you cast an integer to a pointer?
- From: Richard Maine
- Re: How do you cast an integer to a pointer?
- From: glen herrmannsfeldt
- Re: How do you cast an integer to a pointer?
- From: Craig Powers
- Re: How do you cast an integer to a pointer?
- From: James Van Buskirk
- How do you cast an integer to a pointer?
- Prev by Date: Re: pros and cons of user-defined types
- Next by Date: Re: pros and cons of user-defined types
- Previous by thread: Re: How do you cast an integer to a pointer?
- Next by thread: Re: How do you cast an integer to a pointer?
- Index(es):
Relevant Pages
|