请记住结构和指向结构的指针之间的区别。
让我们假设一个 32 位编译器。
让我们创建一个结构:
struct point_i {
int x;
int y;
};
这个结构有两个 int 成员。每个整数大小为 4 个字节,因此结构体大小总共为 8 个字节。
然后使用结构:
strut point_i my_point; // 8 bytes allocated, lets assume that they
// are located at address 0x10000000.
my_point.y = 10;
当你这样做时,编译器知道 my_point 的位置和它的大小,它还知道成员y
相对于结构的位置。所以它(非常粗略地)编译为:
MOV [0x10000004], 10 ;; Notice that its 0x10000000 + 4.
;; The first four bytes are X so we skip them
;; to get to Y and put 10 in that memory address.
另一方面,当您有指针时:
strut point_i *another_point; // 4 bytes allocated, the pointer size.
// Let's assume in 0x20000000.
another_point = get_random_point(); // Get an address to some random point.
another_point->y = 10; // You have to use -> to reference the member
// because you are not dealing with an struct
// anymore but a *pointer* to said struct.
而且由于编译器不知道您将在该指针中放入什么地址,因此它必须生成有点不同的代码。
MOV EBX, [0x20000000] ;; 0x20000000 has your pointer. So we fetch it.
MOV [EBX+4], 10 ;; Dereference the pointer and put 10 in Y.
;; You can see that we now have two memory references,
;; one to get the pointer and another to get where it
;; points to. So it is a layer of indirection.
请注意,这是一个非常简化的世界视图。编译器/链接器和操作系统解析程序上的内存地址。但它应该澄清法院背后发生的事情。指针解引用是 C 语言的主要部分。