|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Mar 2005
Città: /home/gica
Messaggi: 1653
|
[C] Puntatori void e confronti
Ciao a tutti!
Ho gia' chiesto su un altro forum circa questo problema, ma non ne sono venuto a capo. Sono in ambiente Linux e sto usando la funzione Codice:
void* shmat(int shmid, const void *shmaddr, int shmflag); In un programma e' possibile fare questa cosa qui: Codice:
if (shmat(<parametri>)<0) { ... } else { ... } Se pero' sono io a definire una mia funzione del tipo Codice:
void* Funzione() { return -1; } Codice:
if (Funzione()<0) { ... } else { ... } Qualcuno sa spiegarmi come mai questa diversita' di comportamento tra la shmat e Funzione(), nonostante entrambe ritornino un void*? Grazie, Gica ![]() |
![]() |
![]() |
![]() |
#2 | |
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
Non è una buona idea però confrontare così. Io metterei if ((long)shmat(<parametri>)==-1). Questa differenza dovrebbe spiegarti anche il punto successivo (l'if senza cast non ti funziona in quanto void * è più logicamente un "unsigned long" che un "long").
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
![]() |
![]() |
![]() |
#3 | |
Senior Member
Iscritto dal: Mar 2005
Città: /home/gica
Messaggi: 1653
|
Quote:
Ho dato un'occhiata ai sorgenti di shmat per vedere come e' fatta l'istruzione return, ma non ci ho capito una mazza! Codice:
void * shmat (shmid, shmaddr, shmflg) int shmid; const void *shmaddr; int shmflg; { void *__unbounded result; void *__unbounded raddr; #if __BOUNDED_POINTERS__ size_t length = ~0; struct shmid_ds shmds; /* It's unfortunate that we need to make another system call to get the shared memory segment length... */ if (shmctl (shmid, IPC_STAT, &shmds) == 0) length = shmds.shm_segsz; #endif result = (void *__unbounded) INLINE_SYSCALL (ipc, 5, IPCOP_shmat, shmid, shmflg, (long int) __ptrvalue (&raddr), __ptrvalue ((void *) shmaddr)); if ((unsigned long) result <= -(unsigned long) SHMLBA) result = raddr; return BOUNDED_N (result, length); } Ciao |
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Mar 2005
Città: /home/gica
Messaggi: 1653
|
Nessun' altra idea?
![]() ![]() |
![]() |
![]() |
![]() |
#5 | |
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
Nota che il confronto "<0" è sbagliato in ogni caso: puntatori validi sono ad es. 0x80ae7f00, che se interpretati come numeri con segno sono <0!
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 06:20.