I created a sample C++ program on a 64 Bit linux machine running on VMware Player over host machine as windows. The below is a class code.
class operatorOverloadingTest{
private:
int age;
char *name;
public:
operatorOverloadingTest(int age){
this->age = age;
name = new char[10];
strncpy(name,"Indra",strlen("Indra"));
}
void displayDetails(){
cout<<"My Name :"<<name<<endl;
cout <<"My age: "<<age<<endl;
}
friend ostream & operator<<(ostream &out, const operatorOverloadingTest &myObj);
~operatorOverloadingTest(){
delete name;
cout<<" \nDestructor getting called"<<endl;
}
};
In my Main function, created object of class:
1. By using new operator.
2. Stack Object.
int main(){
operatorOverloadingTest *oOT = new operatorOverloadingTest(10);
operatorOverloadingTest oOT1(30);
I understand that, on 64-Bit machine, memory address are represented in 8 Bytes.
When I run the program using GDB, i see the below address:
p oOT
$1 = (operatorOverloadingTest *) 0x613c20
p &oOT
$2 = (operatorOverloadingTest **) 0x7fffffffe228
&(oOT1.age)
$7 = (int *) 0x7fffffffe210
(gdb) p &(oOT->age)
$8 = (int *) 0x613c20
My question is, why does object allocated in heap shows a 32 bit address representation and the object on stack (oOT1) shows a 64 bit address representation though my operating System is 64 Bit? (Checked using uname -a).