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?

- 41,632
- 12
- 85
- 103

- 317
- 2
- 5
- 17
-
you should search for "pointing arithmetic" – Ruggero Turra Oct 11 '12 at 22:40
-
Although the answers you're getting are likely to work in practice, you should be aware that if the two input pointers do not point to elements in the same array, the result of subtracting them is undefined. If they are both pointers into an int array, the result of the subtraction will be in units of sizeof(int), not bytes. – Jim Lewis Oct 11 '12 at 22:41
-
basically: don't do that. If you want to detect the size of an array with that, please: do yourself a favour and pass the size separately. – stefan Oct 11 '12 at 22:43
-
@JimLewis, ??? If both have memory addresses, subtracting them will work just fine. The *result* is not guaranteed to be useful in any way shape or form, but the operation should work just fine – riwalk Oct 11 '12 at 22:43
-
@Stargazer: The standard calls this out as "undefined behavior", which means that a conforming implementation may yield the result you're expecting, or dump core, or send nasty email to your boss. As I said, it's likely to work in practice, but it's good to be aware of the potential problems. – Jim Lewis Oct 11 '12 at 22:49
-
Could you provide some details about what you're trying to do? I'd rather not make an assumption. – Mike Oct 12 '12 at 03:48
-
Typecast the pointers to their coresponding integer types and do the math. – Sandrious May 04 '23 at 12:29
3 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.

- 14,033
- 6
- 51
- 68
-
Pointer difference is of type `ptrdiff_t`. Casting it to `int` may get one into trouble. – Alexey Frunze Oct 12 '12 at 05:49
-
We are talking about security hacking. The part I am currently working on is to print the stack and the values at the locations in the stack from a certain point in memory to another point. Since the addresses of these locations in memory can change each time a program is executed, we were instructed to use an offset because that will always stay the same no matter what computer it is run on. I mentioned one address in the original post and another in the comment on the previous answer. – Matt Altepeter Oct 12 '12 at 13:03
-
Actually, those are not the correct addresses. The addresses are: 0xbfa00be4 and 0xbfa00b50. – Matt Altepeter Oct 12 '12 at 13:13
I'd really like more details about how you're using this information. That could make for a much more concise answer.
Anyway. normally what happens:
int a = 1;
int b = 2;
int * w = &a; //0xbfdfa900 - These are right next to each other on the stack since
int * x = &b; //0xbfdfa904 they were declared together
int y = (int)w;
int z = (int)x;
int diff = w - x; // There's a 4 byte difference in memory, but I'd get diff = 1
// here because the compiler knows they're ints so I'm getting
// diff/sizeof(int)
int pdiff = y - z; // Now I'm going to get the number of bytes difference, so
// pdiff = 4 as this is due to using the address as a raw value
There's how to get the two different offsets between two pointers. Now obviously, if your pointers aren't next to each other on the stack, the values start to change:
int a = 1;
int arr[5] = {0};
int b = 2;
int * w = &a; //0xbfdfa900 - These are right off by 24 bytes (6 * sizeof(int))
int * x = &b; //0xbfdfa918 because we have 5 more ints there
The more distance and different types between the two the more we lose any obvious "offset" between two variables, in other words this starts to become pointless. This is why pointer arithmetic really only works on arrays (because they are known contiguous memory of a particular type). So as in your case:
int * one = &somenum; // 0xbfde61e0
int * two = &someothernum; // 0xbfbf69e0
printf("%d\n", (int)two-(int)one);
2029568 bytes
These are a good distance away. So you can subtract them, but I'm not sure why you'd do this.

- 47,263
- 29
- 113
- 177
-
This code might fail miserably on 64 bit system where an `int` is 4 bytes only. Use `uintprt_t` or `intptr_t` for `y` and `z` instead of `int`. – alk Jun 17 '18 at 18:01
I think this gets the offset.
int *a = &somevar;
int *b = &anotherintvar;
int offset = b - a;

- 10,907
- 4
- 48
- 72
-
doesnt that give the offset in units of sizeof(int) as opposed to bytes (maybe thats what he wants) – pm100 Oct 11 '12 at 22:40
-
The two addresses in question are the one mentioned above, and this: 0xbfbf69e0. When performing the calculation and printing it I get -63936. This doesn't seem like the proper value. I subtract the one mentioned in this comment from the one listed above. – Matt Altepeter Oct 11 '12 at 22:42
-
I figured out my issue. Silly mistake on my part. I ran the programs separately so the addresses were not in the same array. – Matt Altepeter Oct 12 '12 at 13:17