Hello,
Attached are two codes.
Please compile them and check the output.
The question I have is that I am unable to understand why is it that the memory location allocated to tempmemcheck are 8bytes + the last memory location and not 4 bytes plus the last memory location.
Similarly, why is the memory location of ver2 on heap 16+ memory location of ver1->tempmemcheck and why not 4 + memory location of ver1->tempmemcheck.
I am using g++ of gcc version 4.1.1 20061011 (Red Hat 4.1.1-30)
In anticipation of an enlightening reply..
main.cpp:
#include <iostream>
#include "graph.h"
using namespace std;
int main()
{
int a=5;
int *b=new int;
*b=6;
vertex *ver1=new vertex(5);
vertex *ver2=new vertex(5);
printf("Here comes all the address listings:\n");
printf("variable a: %x\n",&a);
printf("variable b: %x\n",&b);
printf("memory location alloted to int b: %x\n",b);
printf("variable ver1: %x\n",&ver1);
printf("memory location alloted to struct ver1, known by its members: %x %x %x\n" ,&(ver1->id),&(ver1->status), &(ver1->tempmemcheck));
printf("memory location alloted to struct ver1, known directly : %x\n", ver1);
printf("Memory location alloted to member tempmemcheck of struct ver1: %x\n",ver1->tempmemcheck);
printf("variable ver2: %x\n",&ver2);
printf("memory location alloted to struct ver2, known by its members: %x %x %x\n" ,&(ver2->id),&(ver2->status), &(ver2->tempmemcheck));
printf("memory location alloted to struct ver2, known directly : %x\n", ver2);
printf("Memory location alloted to member tempmemcheck of struct ver2: %x\n",ver2->tempmemcheck);
}
graph.h in the same directory as main.cpp
class vertex
{
public:
int id;//ID of the vertex;
int status;//Status of the vertex;
int *tempmemcheck;
vertex(int n)
{
id=n;
status=0;
tempmemcheck=new int;
}
};