How does the compiler understand the type of pointer?

How does the C ++ compiler understand the type of pointer? As I know, a pointer has a size equal to OS WORD (32 or 64). So does it store some information in this 32 (or 64) type bits? Just because you do not have a pointer to one type and assign that pointer a different pointer with a different type.

+3
source share
7 answers

A pointer is usually just a memory address on an x86-based architecture (I don't know of other architectures). The compiler provides type safety with different pointers at compile time - because it makes no sense to assign a char pointer to a pointer to an object, for example, especially since the specified objects have different sizes (so you'd capture random memory if you accessed them). You can explicitly override this and assign any pointer to any other pointer using reinterpret_cast<T>or with other types of castings, such as static_cast<T>and dynamic_cast<T>(the last two are usually recommended because they are safer, but each of them has its own uses).

, , . , , , . , , .

(-, STL) , , : , , , , , .. , , .

+4

, , , :

int* ip;   // ip is a pointer to an int

float* fp; // fp is a pointer to a float

void* vp;  // vp is a pointer to some unknown type; need to cast it to a pointer
           // to an actual type in order to access the pointed-at object
+10

, "", , .

, , , ( ), , , , . , :

int*    ip;
// do some stuff
double* dp = ip;

- ( ):

... "ip". , . , "dp". , . , ip dp. ... ! ip - , dp - . !

... vomit ...

, ( , - ) ( , ).

+3

. :

dest = source
// make sure that type of source == type of dest
+1

, .

, ( C/++ ), , . ASM (, , ..), , , - int, a char - .

( , , .)

, , , . .

+1

, WORD OS

CPU word ? word , .

, .

. :

0
0

Source: https://habr.com/ru/post/1749941/


All Articles