指针的底层实现

How Are Pointers Implemented In The Machine?

How are pointers implemented?
The short explanation is that every area of memory in themachine has a numeric address like 1000 or 20452. A pointer to an area of memory isreally just an integer which is storing the address of that area of memory.
The dereferenceoperation looks at the address, and goes to that area of memory to retrieve the pointeestored there.

Pointer assignment just copies the numeric address from one pointer toanother.The NULL value is generally just the numeric address 0 — the computer justnever allocates a pointee at 0 so that address can be used to represent NULL.

A badpointer is really just a pointer which contains a random address — just like an
uninitialized int variable which starts out with a random int value. The pointer has not
yet been assigned the specific address of a valid pointee. This is why dereference
operations with bad pointers are so unpredictable. They operate on whatever random area
of memory they happen to have the address of.


Reference:

http://cslibrary.stanford.edu/102/PointersAndMemory.pdf

END

你可能感兴趣的:(指针)