0

The code works as expected until the lines 22-24, where we are printing 8 followed by address. Incrementing the pointer address increments the address by one byte only, whereas it should move address by 4 bytes. The problem does not occur in arrays or if lines 22-24 are run separately.

#include<iostream>
using namespace std;

void main()
{
   int *p;
   //int a[10] = { 0 };
   //p = a;
   int a = 100;
   p=&a;
   cout << "1.    "<<p <<"    "<<*p<< endl;
   p++;
   cout << "2.    " << p << "    " << *p << endl;
   ++p;
   cout << "3.    " << p << "    " << *p << endl;
   ++*p;
   cout << "4.    " << p << "    " << *p << endl;
   ++(*p);
   cout << "5.    " << p << "    " << *p << endl;
   ++*(p);
   cout << "6.    " << p << "    " << *p << endl;
   *p++;
   cout << "7.    " << p << "    " << *p << endl;
   (*p)++;      //This is the problem, increments the address by 1,    even though its in int type
   cout << "8.    " << p << "    " << *p << endl;
   *(p)++;
   cout << "9.    " << p << "    " << *p << endl;
   *++p;
   cout << "10.    " << p << "    " << *p << endl;
   *(++p);
   cout << "11.    " << p << "    " << *p << endl;
   cin.get();
}

2 Answers2

0

Initially you set p to point to an integer variable on the stack. When you subsequently increment the pointer you are pointing to an area of memory on the stack which is likely to change when a function is called ( cout for example). when the function returns it will probably have changed the memory location that your incremented pointer p is pointing to and this probably explains your issue. You should declare an array large enough to accommodate the range of pointer addresses that you are going to step through. I notice that you commented out the array code which would have worked as you expected.

0

Your code is:

p = &a;
p++;

Now p is pointing past the end of a. This is still OK, however on the next line:

cout << "2.    " << p << "    " << *p << endl;

when you write *p this tries to read the memory past the end of a, which causes undefined behaviour.

When undefined behaviour has happened, the definition of the C++ language no longer covers what the program does. Anything can happen.

To put it another way: when generating the executable, the compiler can make assumptions based on the premise that your program only does things which are well-defined.

Your output could perhaps be explained by the compiler making such an assumption which would be justified if your program were confirming, but is actually false because your program is invalid.

One explanation that comes to mind is that you advanced p until it happens to be pointing to the memory location in which p itself is stored. The compiler implements (*p)++ by outputting an instruction for incrementing an int stored at the location where p is pointing. On your system, the result of applying this instruction to the location where actually p is stored is to increase the address value of p by one.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • Try fixing your program so it never reads or writes to invalid memory (and doesn't have any other instances of UB) and you should find that the strange output goes away. (If not, then post the code you are running along with a copy-paste of the actual output). – M.M Mar 22 '15 at 00:46