Coupon Accepted Successfully!


Specifying Address Of An Object

The compiler assigns an address to the object created. The programmer has no power over the address of the object. However, it is possible to create an object at the memory location given by the program explicitly. The address specified should be big enough to hold the object. The object should be created in such a way that it must be destroyed by invoking the destructor explicitly. The following program makes the concept clearer:

14.14 Write a program to create object at given memory address.



class data


int j;


data ( int k) {j=k;}

~data() { }

void *operator new ( size_t, void *u)


return (data *)u;


void show() {cout<<“j=”<<j;}


void main()



void *add;


data *t=new (add) data(10);

cout<<“\n Address of object:”<<t;





Address of object : 0x8f800420


In the above program, the new operator is overloaded. The void pointer *add is declared and initialized with address 0x420. The pointer object *p is declared, and an address is assigned to it. The new operator is also invoked to allocate memory. In the same statement, the constructor is also invoked, and a value is passed to it. The member function show() displays the value of a member variable. Finally, the statement t->data::~data(); invokes the destructor to destroy the object. We can confirm the address of the object by displaying its address. The address of the object would be the same as the specified one.

Test Your Skills Now!
Take a Quiz now
Reviewer Name