Address of a class

T

toton

Hi,
How to find address of a class from inside?
My class having a overloaded new & delete.

new allocates the memory like
void* operator new(std::size_t size) throw(){
cout<<"op new called"<<endl;
void* ptr = std::malloc(size);
if(!ptr){
throw std::bad_alloc();
}
return ptr;
}
delete is like
void operator delete(void* ptr) throw(){
cout<<"op delete called"<<endl;
if(ptr){
std::free(ptr);
}
}
Now is &ptr is the location of the object in memory for new & delete ?
From within class how to get it? &*this ?
from outside the class?
Test* t = new Test(); is how it is initialized. now &t is the memory
location of the class?
These values are not same in the class.... What is the mistake I am
doing here?

The class also has a member variable as pointer to array, like
class Test{
private:
Client* client;
}
after initialization, how to find the location of client from within
the class , &client?
It will be helpful if anyone points out about the addresses. I am
interested in how the class layouts in memory....

thanks
abir
 
T

toton

toton said:
Hi,
How to find address of a class from inside?
My class having a overloaded new & delete.

new allocates the memory like
void* operator new(std::size_t size) throw(){
cout<<"op new called"<<endl;
void* ptr = std::malloc(size);
if(!ptr){
throw std::bad_alloc();
}
return ptr;
}
delete is like
void operator delete(void* ptr) throw(){
cout<<"op delete called"<<endl;
if(ptr){
std::free(ptr);
}
}
Now is &ptr is the location of the object in memory for new & delete ?
from outside the class?
Test* t = new Test(); is how it is initialized. now &t is the memory
location of the class?
These values are not same in the class.... What is the mistake I am
doing here?

The class also has a member variable as pointer to array, like
class Test{
private:
Client* client;
}
after initialization, how to find the location of client from within
the class , &client?
It will be helpful if anyone points out about the addresses. I am
interested in how the class layouts in memory....
Sorry, little error was there...
address of the class from outside &(*t) ?
address of the class from inside &(*this) ?
address of the class from inside new / delete?
address of the pointer of the class
from outside &t?
 
R

Rolf Magnus

toton said:
Hi,
How to find address of a class from inside?

Classes don't have addresses. They are a concept that only exists during
compile time. You probably mean 'address of an object'.
My class having a overloaded new & delete.

new allocates the memory like
void* operator new(std::size_t size) throw(){
cout<<"op new called"<<endl;
void* ptr = std::malloc(size);
if(!ptr){
throw std::bad_alloc();
}
return ptr;
}
delete is like
void operator delete(void* ptr) throw(){
cout<<"op delete called"<<endl;
if(ptr){
std::free(ptr);
}
}
Now is &ptr is the location of the object in memory for new & delete ?

&ptr is the address of the pointer, not the object.

Well, yes. But you can leave out the &*. 'this' is a pointer to the object.
What your code does is first dereference it, then take the address again.
from outside the class?
Test* t = new Test(); is how it is initialized. now &t is the memory
location of the class?

No. &t is the memory location of the pointer. t itself already is the memory
location of the object.
These values are not same in the class.... What is the mistake I am
doing here?

What values?
The class also has a member variable as pointer to array, like
class Test{
private:
Client* client;
}
after initialization, how to find the location of client from within
the class , &client?

Yes, if you really want the location of 'client', i.e. the pointer itself.
If you want what it points to, you must again drop the &.
It will be helpful if anyone points out about the addresses. I am
interested in how the class layouts in memory....

Well, the layout is compiler-specific.
 
T

Thomas J. Gritzan

toton said:
Hi,
How to find address of a class from inside?

The 'this' pointer is the address of the instance of a class.
My class having a overloaded new & delete.

new allocates the memory like
void* operator new(std::size_t size) throw(){

Here you say that operator new does not throw any exception,
cout<<"op new called"<<endl;
void* ptr = std::malloc(size);
if(!ptr){
throw std::bad_alloc();

Then you throw an exception. I bet its undefined behaviour.
Better get the throw() specification right.
 
J

Jerry Coffin

[ ... ]
Here you say that operator new does not throw any exception, [ ... ]
Then you throw an exception. I bet its undefined behaviour.

You'd lose that bet. The behavior is defined -- but usually
undesirable (unexpected() is invoked, and by default it terminates
your program with no further chance at cleanup or anything else.
Better get the throw() specification right.

Better still to eliminate it completely, IMO.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top