2013-09-10 13 views
5

Capisco che questa affermazione sta lanciando un carattere volatile senza segno a un indirizzo di memoria, ma quello che non capisco è il puntatore dopo volatile.Non capisco perché il puntatore utilizzato nel cast

#define PORTC *(unsigned char volatile *)(0x1003) 
+1

Vedete questo su processori in cui una porta I/O è mappato in un indirizzo di memoria specifica, in modo che quando si legge o si scrive 'PORTC' stai scambiando informazioni con il dispositivo esterno. –

risposta

5

Dice: trattare il numero 0x1003 come un puntatore a carattere senza segno volatili; leggere o scrivere il valore (byte) a quell'indirizzo, a seconda di come viene utilizzato:

unsigned char c = PORTC; // read 
PORTC = c + 1;    // write 
5

non è quello che sta succedendo. Piuttosto, interpreta il valore 0x1003 come puntatore e quindi è dereferenziazione a tale puntatore per ottenere un valore di tipo volatile unsigned char. Essenzialmente, questo è un modo per accedere a un byte in una posizione di memoria fissa. (Il "volatile" fa rispettare un vero e proprio "accesso" a quella locazione di memoria, che è un concetto alquanto vagamente definita nello standard.)

0
(TYPE*) POINTER 

è una sintassi che viene interpretato come un tipo-cast di puntatore. Ad esempio,

int m = 4; 
char* p_char = (char*)&m; 

Così, p_char è un puntatore a char. Se si denota p_char, ovvero *p_char, la rappresentazione binaria nella posizione in cui p_char punta verrà trasformata in una rappresentazione di char (carattere). Il risultato esatto di questo valore non è definito. Non ho idea di quale valore esatto tornerà, ma restituirà qualcosa come un personaggio strano č.

Per una comprensione più approfondita del puntatore, voglio sottolineare che un puntatore è semplicemente una delle interfacce per accedere all'entità rappresentata nel linguaggio C++ e residente nella memoria del computer.

#include <iostream> 
using namespace std; 

/* 
* C++ interface to represent entity which resides on computer memory: 
*  ---------            
* 1) object; 
* 2) pointer; 
* 3) reference; 
* 
* C++ interpretation of entity through interface: TYPE 
*  --------------        ----   
* 
* What is the function of TYPE? 
* 1) tell compiler the size of an object of this TYPE needed; (sizeof(int) -> 4 bytes) 
* 2) when the value of object at which it resides is dereferenced, 
*  the binary represented value is transformed to some other 
*  representation value according to the TYPE's interpretation rule; (if int, interpret it as an int) 
* 
* 
*    +----------------+   
*    |  0x02105207 |      
*    |  0x02105206 |      
*    |  0x02105205 |      
*  int n |  0x02105204 |      
*    +----------------+      
*    |  0x02105203 |      
*    |  0x02105202 |      
*    |  0x02105201 |      
* ---->int m |  0x02105200 |      
* |   +----------------+     
* |   |    |      
* |   +----------------+     
* |   ...    ...      
* |   +----------------+     
* ---- int* p |  0x00002298 |      
*    +----------------+     
* 
* if the pointer in figure is declared as: 
* 
*  int* p = &m; 
* 
* the face of 0x00002298 -> 0x02105200 will not be changed until p is 
* assigned to other address value; 
* 
* 
*/ 

class consecutive_two_int 
{ 
public: 
    consecutive_two_int():m(4),n(123){} 
    int m; 
    int n; 
}; 

int main() 
{ 
    int * p; 
    consecutive_two_int obj; 
    p = &(obj.m); 

    for(int i = 0; i < 8; ++i) 
    { 
     // because pointer of char progresses every 1 byte; 
     // increment memory byte by byte through it; 
     cout << *(int*)((char*)p + i) << "\n"; 
    } 

    return 0; 
} 

Il risultato, ad esempio, è:

4 // the first defined int 
2063597568 // undefined 
8060928 // undefined 
31488 // undefined 
123 // the second defined int 
268435456 // undefined 
1175453696 // undefined 
-465170432 // undefined 
Problemi correlati