Reputation: 141
I am trying to execute this C++ Program code in TurboC++
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{clrscr();
int i;
long int a=10,*p;
p=&a;
for(i=0;i<10;i++)
{printf("\n{Via printf}\t&a=%p,p=%p",&a,p);
cout<<"\n{Via cout}\t&a="<<&a<<",p="<<p;
p=p+1;
}
getch();
}
Now, output of this program is as follows:
{Via printf} &a=FFF2,p=FFF2
{Via cout} &a=0x8f87fff2,p=0x8f87fff2
{Via printf} &a=FFF2,p=FFF6
{Via cout} &a=0x8f87fff2,p=0x8f87fff6
{Via printf} &a=FFF2,p=FFFA
{Via cout} &a=0x8f87fff2,p=0x8f87fffa
{Via printf} &a=FFF2,p=FFFE
{Via cout} &a=0x8f87fff2,p=0x8f87fffe
{Via printf} &a=FFF2,p=0002
{Via cout} &a=0x8f87fff2,p=0x8f870002
{Via printf} &a=FFF2,p=0006
{Via cout} &a=0x8f87fff2,p=0x8f870006
{Via printf} &a=FFF2,p=000A
{Via cout} &a=0x8f87fff2,p=0x8f87000a
{Via printf} &a=FFF2,p=000E
{Via cout} &a=0x8f87fff2,p=0x8f87000e
{Via printf} &a=FFF2,p=0012
{Via cout} &a=0x8f87fff2,p=0x8f870012
{Via printf} &a=FFF2,p=0016
{Via cout} &a=0x8f87fff2,p=0x8f870016
I know 0x (i.e, p output via cout) is denoting hexadecimal no. ,
also its last four digits is denoting its stored value,
but what does 8f87 (its third,fourth,fifth & sixth digits) is denoting?
Upvotes: 2
Views: 132
Reputation: 30010
Both printf
and cout
prints the value of the pointer.
printf
just prints the offset part, while cout
prints both the segment and offset part of the pointer (even if segment is meaningless, as in some memory models, segment is not stored in the pointer. In this case, the value of a segment register is printed, perhaps DS
).
0x8f87fff2 can be split into segment:offset notation: 8f87:fff2, where 8f87 is the segment, and fff2 is the offset.
Upvotes: 2
Reputation: 11968
Don't try to interpret pointer values, unless you are writing hardware drivers.
In general pointers are just 32 or 64 bit numbers that denote some byte in memory. Depending on the system you run on you will get different values. You shouldn't ever care what the value of the pointer actually is. You should care that the address it points to is valid (not null, and the data there was not deleted).
Good programs will work regardless of how the memory is allocated.
The difference between printf and cout is likely due to how they are implemented. TurboC++ is ancient by any measure so I can't give you a full answer on why you see the difference.
One notable exception is doing low level interaction with drivers that are memory mapped. This usually works by mapping a certain range of addresses to certain hardware device and modifying data in those ranges will instruct the hardware to take certain actions.
Upvotes: 0
Reputation: 26800
but what does 8f87 (its third,fourth,fifth & sixth digits) is denoting?
The standard states that in its discussion of compound types:
The value representation of pointer types is implementation-defined.
So we cannot say for sure what those digits represent.
As others have pointed out in the comments, Turbo C++ is very old and you would do better to use free compilers like GCC or Clang.
In GCC, both cout and printf print exactly the same. See Demo.
Upvotes: 0
Reputation: 27577
The whole pointer address is being output in hexadecimal, why printf
and cout
are outputting different values is an implementation issue.
This is where your data is being stored, it has nothing to do the value of that data:
long int a=10,*p;
p=&a;
&a; // where that data is stored
p; // where that data is stored
a; // value of that data
*p; // value of that data
Upvotes: 2