1

I would expect the values of &n and &n + 1 to be adjacent memory boxes, so their address should differ by 1. However, every time I run these commands, I get addresses that differ by 4 (for example 0056F800 and 0056F804). Why does this happen?

#include <iostream>
using namespace std;

int main()
{
    int n = 3;
    cout << &n << endl;
    cout << &n + 1;
}
cigien
  • 57,834
  • 11
  • 73
  • 112
Ovi
  • 573
  • 2
  • 5
  • 16
  • 2
    What is `n`? A [mcve] would be helpful. – Eljay Jul 11 '21 at 21:25
  • 3
    Hint: `sizeof(*n)`. – orlp Jul 11 '21 at 21:27
  • @Eljay I've added an example. – Ovi Jul 11 '21 at 21:28
  • 1
    When you say "box", you're effectively saying "byte". Do you believe integers are no larger than a single byte? If they are larger, then they must be separated by more than one byte to avoid overlap. – Tom Karzes Jul 11 '21 at 21:29
  • Food for thought: If you make it 1, you have to choose between `int* + int*` not resulting in `int*` or ending up with some `int*` that points into the middle of an `int`. Both of those are really awkward. – chris Jul 11 '21 at 21:30
  • @TomKarzes Oh so `&n` is the address of the first byte of `n`, but then `&n + 1` is the address after the last byte of `n`. Intuitive in a way, but counterintuitive in another. – Ovi Jul 11 '21 at 21:40
  • @chris I'm sorry, I don't fully understand. What exactly is `int*`? – Ovi Jul 11 '21 at 21:41
  • @TomKarzes Nevermind, I don't think my explanation is a good way of thinking about it. I would've thought that `&n + 2` is `&n` incremented by `5`, but I see it's actually incremented by `8`. – Ovi Jul 11 '21 at 21:46
  • @Ovi When adding an integer to a pointer, the integer is multiplied by the size of the object the pointer points to. The compiler performs this multiplication automatically. So if you have `int *p;` and `int i;`, then when you do `p + i`, the actual pointer addition is `p + sizeof(*p)*i`, which in this case is `p + sizeof(int)*i`. – Tom Karzes Jul 11 '21 at 22:09
  • @TomKarzes Yup got it, thanks! – Ovi Jul 11 '21 at 22:14
  • @Ovi, `int*` is a pointer to an `int`. In `int* + int*`, it's meant as a stand-in for any value of that type since my statement was just focusing on types. – chris Jul 12 '21 at 01:05
  • @chris It seems to me that the addresses of `int` variables are always numbers divisible by `4`. Are you saying that if you add two addresses of ints, then you get an address that is a *feasible* address for another int (since the sum of two multiples of `4` is a multiple of `4`)? I tried adding two pointers in C++ btw and the debugger says that I can't add two pointers. – Ovi Jul 12 '21 at 18:03
  • @Ovi, Oops, I did mean to add a number, not a second pointer. I guess that's what I'm saying though, put differently. If it's up to the programmer to remember to multiply by `sizeof(int)` every time they do pointer arithmetic, it would be easy to forget, producing a result that points to an unintended address (quite possibly partially through an existing `int` object). It's also rather rare to want anything except multiples of `sizeof(int)`, meaning that multiplication by `sizeof(int)` on almost every pointer artihmetic expression would end up as repetetive boilerplate. – chris Jul 13 '21 at 03:53
  • @chris Oh I completely understand now, thanks! – Ovi Jul 13 '21 at 03:56

1 Answers1

4

Pointer arithmetic creates a pointer to a specific element of an array (or equivalently, a single object which is treated as an array of size 1), so it effectively changes the value of the pointer by multiples of the element size.

On your system an int is apparently 4 bytes in size so by adding 1 to an int * it creates a pointer value 4 larger than the original value.

dbush
  • 205,898
  • 23
  • 218
  • 273