-3

This is the program

int main() {   
    cout << sizeof(int) << endl;        // for int its 4 in g++ compiler

    int *p;
    int a = 5;
    p = &a;
    cout << "The value of p is: " << p << endl;
    cout << "The value of p + integer is: " << p + 0 << endl;

    // lets take the size of individual 1, 2, 3
    cout << "The sizeof(0) is: " << sizeof(0) << endl;   // 4
    cout << "The sizeof(1) is: " << sizeof(1) << endl;   // 4
    cout << "The sizeof(2) is: " << sizeof(2) << endl;   // 4

    cout << "The value of p + 0 is: " << p + 0 << endl;
    cout << "The value of p + 1 is: " << p + 1 << endl;
    cout << "The value of p + 2 is: " << p + 2 << endl;

    return 0;
}

The sizeof() function in C++ gives sizeof(int) 4 bytes, in g++ compiler. So I printed the sizeof(1), sizeof(2), sizeof(0) to terminal and I got 4 bytes.

So I tried some pointer arithmetic in the program in above link. I added 1 to a pointer variable. Let's say int *p; int a = 10;. Now I assigned p = &a;. Now when I printed p it gives 0x24fe04 and when I printed p + 0 it's the same. But when I tried adding p + 1 and p + 2 it gives different output like this: 0x24fe08, 0x24fe0c respectively. Please help me understanding this arithmetic. Why p+1, p+2 is not equal as in address it's contributing the same 4 bytes.

SimpleGuy
  • 2,606
  • 4
  • 23
  • 39
an0nh4x0r
  • 424
  • 7
  • 18
  • if you have `int *p;` then `p+=1;` in fact means add `1*sizeof(int)` to the pointer... – W.F. Aug 20 '16 at 15:17
  • 2
    What output did you expect? – n. 1.8e9-where's-my-share m. Aug 20 '16 at 15:18
  • `0x24fe08 + 0x000004 = 0x24fe0c`. Adding `1` in pointer arithmetic is equivalent to move `sizeof(T)` memory positions. In your example `T` is `int` where `sizeof(int) == 4` this means that `p + 1` will move four memory positions ahead and thus you get `0x24fe0c` and not `0x24fe09` – 101010 Aug 20 '16 at 15:18
  • if `p` is `0x24fe04`, then p+1 will be `0x24fe08` and p+2 would be `0x24fe0c ` which is what you mentioned and it is right. Pointer moves ahead by an `address + sizeof(type-of-pointer)`, which is `int` in your case. And as you mentioned `int` is `4 bytes`, so addresses are incremented by four bytes. – SimpleGuy Aug 20 '16 at 15:20
  • @BenjaminLindley Reopening that without providing on of the other dupes appearing here in myriads, wasn't the right action. – πάντα ῥεῖ Aug 20 '16 at 15:22
  • but the sizeof(1) == sizeof(2), that is 4 bytes. so adding 1 or 2 or any integer to a pointer to integer variable must give the same result. is the address of p printed in console in bytes ? is the new address after arithmetic operations is (actual integer value) * 4 + address ? – an0nh4x0r Aug 20 '16 at 15:22
  • @πάνταῥεῖ: There's probably a duplicate of this question, but it certainly wasn't the one you linked. This question has absolutely nothing to do with arrays. Undoing your invalid dupe vote was the right action. – Benjamin Lindley Aug 20 '16 at 15:23
  • 2
    adding `sizeof(1)` or `sizeof(2)` would indeed give same result. `p + sizeof(1)` or `p + sizeof(2)` would give same result. But you are adding `p + 1` and `p + 2` which is different – SimpleGuy Aug 20 '16 at 15:24
  • 1
    It's pointer arithmetic basics. Increasing a pointer with a value, like you do with p+1, p+2, will yield an address aligned to the pointer size (in your case, 32-bits). If you want to increase the value the pointer points at (in your case, the contents of integer 'a') you should dereference the pointer with the * operator. To resume, p+X will increase the value (address) of the pointer, aligned to the pointer size. *p+X will increase the value contained in the address pointed by p. *(p+X) will increase the pointer and dereference later, naturally. – Hernán Aug 20 '16 at 15:28
  • This isn't the problem, but don't use `std::endl` unless you need the extra stuff that it does. `'\n'` ends a line. – Pete Becker Aug 20 '16 at 15:37
  • Thanks alot guys. But how p+1 is different from p + size(1) @SimpleGuy. – an0nh4x0r Aug 20 '16 at 15:51
  • @an0nh4x0r `p + 1` is `p + 1` whereas `p + sizeof(1)` is `p + 4`. – Biffen Aug 20 '16 at 15:54
  • @Biffen but in p + 1, 1 was integer and in p + sizeof(1) which you're saying is equal to p + 4, here 4 is in bytes right ? – an0nh4x0r Aug 20 '16 at 15:57
  • @an0nh4x0r `sizeof` evaluates to a numeric value, in this case 4. Yes, that means four bytes, but it's still just the number 4. – Biffen Aug 20 '16 at 15:59
  • @Biffen Thank you very much. Finally i understood :) – an0nh4x0r Aug 20 '16 at 16:02

2 Answers2

0

A pointer is exactly that, it points to another area of memory.

int a = 3; 
int *p = &a;
++a;
std::cout << p << std::endl;    
std::cout << *p << std::endl;
std::cout << &p << std::endl;
std::cout << &a << std::endl;

should output:

The address of a
4
The address of p
The address of a

If you do p+1, you are moving 4 bytes up the memory stack from a's address because on 32-bit machines memory is indexed to that alignment. On 64-bit machines it has double that alignment hence you can't mix 32-bit and 64-bit code due the memory index differences.

You also have pointer pointers, so you can manipulate pointers

int **pp = &p
std::cout << *pp; // = 4 = p = a

Edit

Should have checked that, my mistake, I've fixed the above thanks to Benjamin Lindley

clu
  • 1
  • 2
0

When you are saying p + 1, meaning that p + 1 * sizeof(int). As I've mentioned here, the formula is

                                 =  +  × 

That's why you get 0x24fe04 while trying p + 0, and get 0x24fe08 for p + 1

Community
  • 1
  • 1
snr
  • 16,197
  • 2
  • 61
  • 88