得到两个内存地址之间的差异 [英] Getting the difference between two memory addresses

查看:76
本文介绍了得到两个内存地址之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的内存地址为一个int *:0xbfde61e0.我还有另一个内存地址(也为int *.如何计算两者之间的差值以用作两个位置之间的偏移量?

I have the memory address of one int *:0xbfde61e0. I also have another memory address (that is also int *. How can I calculate the difference between the two to use as an offset between the two locations?

推荐答案

听起来很简单.

int a = 5;
int b = 7;

int *p_a = &a;
int *p_b = &b;

int difference = p_b - p_a;

请记住,这将使差异为sizeof(int)的倍数.如果您希望字节不同,请执行以下操作:

Keep in mind that this will give the difference as a multiple of sizeof(int). If you want the difference in bytes, do this:

int differenceInBytes = (p_b - p_a) * sizeof(int);

没有特定的代码或特定的应用程序,我无法比这更详细.

Without specific code or a specific application, I can't get more detailed than that.

这篇关于得到两个内存地址之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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