当我减去内存地址,为什么比我预想的结果更小? [英] When I subtract memory addresses, why is the result smaller than I expected?

查看:105
本文介绍了当我减去内存地址,为什么比我预想的结果更小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程序:

#include <iostream>

struct X
{
    int a;
    float b;
} x[10], *p1, *p2;

int main(int argc, char *argv[])
{
    p1 = &x[1];
    p2 = &x[5];

    int i = p2 - p1;

    std::cout << i << std::endl;
}

我可以想像 X 的内存布局,含10盒的 INT 浮动 P1 将在第二个框的开头( X [1] )和 P2 在第6框的开头指向( X [5]

I can visualize X's layout in memory, 10 boxes containing an int and a float, p1 will point at the beginning of the second box (x[1]) and p2 pointing at the beginning of the 6th box (x[5]):

   X   0   1  2  3  4  5  6  7  8  9
       _______________________________
    b  |__|__|__|__|__|__|__|__|__|__|
    a  |__|__|__|__|__|__|__|__|__|__|
          |           |    
          |           | 
          p1          p2

是我的画是否正确?如果是这样,为什么是 I 4?结果的结果
有一些困难,两个地址的理解减法?

Is my drawing correct ? If so why is the result of i 4 ?
Having some difficulties understanding subtraction of two addresses ?

推荐答案

这是如何<一个href=\"http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc03ptarith.htm\">pointer算术工作。试想一下:

This is how pointer arithmetic works. Consider:

p1 = (x*)100;   // invalid memory address, just an example!
p2 = p1 + 1; 

在这一点上,<​​code> P2 将不会有值 101 ,而 100 +的sizeof(X)(这让我们说的是8,所以108)。据加一,而是由的一个倍数的sizeof(X)!相反,从一个指针减去一个整数实际上减去的 的倍数的sizeof(尖键入)

At this point, p2 will not have the value 101, but rather 100 + sizeof(x) (which let's say is 8, so 108). It has been incremented not by one, but by one multiple of sizeof(x)! Conversely, subtracting an integer from a pointer actually subtracts multiples of sizeof(the pointed to type).

所以,现在,如果你做 INT差异= P2 - P1 ,你当然希望获得 1 回,没有 8 !否则,减去您刚才添加不会产生原始值的数量。因此,从另一个收率不在存储器中的地址的差,但在两个指针之间的元素数减去1的指针。

So now if you do int diff = p2 - p1, you would certainly expect to get 1 back, not 8! Otherwise, subtracting the number you just added would not yield the original value. Therefore, subtracting one pointer from another yields not the difference in memory addresses but the number of elements between the two pointers.

此外,该的标准规定指针减法是没有意义的,除非这两个指针指向的元素在同一阵列中的(更正确,这是不确定的行为,你也允许使用指针一过去的最后一个元素就算是有没有这样的对象)。

Moreover, the standard mandates that pointer subtraction is meaningless unless the two pointers point to elements in the same array (more correctly, it's undefined behavior and you are also allowed to use a pointer to "one past the last element" even if there is no such object there).

最后,如果编译器不知道尖的大小键入的内容(即指针无效* )?在这种情况下,指针运算根本没有被允许的。例如:

Finally, what if the compiler does not know the size of the pointed to type (i.e. the pointers are void*)? In this case, pointer arithmetic is not allowed at all. For example:

void* p = 100;
void* x = p + 1; // does not compile¹

¹一些编译器可以提供关于指针运算无效* 作为的extension 的语言规范。在这种情况下,这种说法确实可以编译,其结果将取决于所述延伸的规格(例如GCC最终会得到价值101)。

¹ Some compilers may provide pointer arithmetic on void* as an extension to the language specification. In this case this statement can indeed compile and the result would depend on the specification of said extension (e.g. gcc would end up with the value 101).

这篇关于当我减去内存地址,为什么比我预想的结果更小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆