Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the difference between two memory addresses

Tags:

c

memory

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?

like image 543
Matt Altepeter Avatar asked Oct 11 '12 22:10

Matt Altepeter


1 Answers

Its as easy as it sounds.

int a = 5;
int b = 7;

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

int difference = p_b - p_a;

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.

like image 83
riwalk Avatar answered Sep 18 '22 14:09

riwalk