473,769 Members | 4,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Address of a class

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(siz e);
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

Aug 1 '06 #1
4 1893

toton wrote:
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(siz e);
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....
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?
thanks
abir
Aug 1 '06 #2
toton wrote:
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(siz e);
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.
>>From within class how to get it? &*this ?
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.

Aug 1 '06 #3
toton schrieb:
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(siz e);
if(!ptr){
throw std::bad_alloc( );
Then you throw an exception. I bet its undefined behaviour.
Better get the throw() specification right.
}
return ptr;
}
delete is like
void operator delete(void* ptr) throw(){
cout<<"op delete called"<<endl;
if(ptr){
std::free(ptr);
}
}
--
Thomas
Aug 1 '06 #4
In article <ea**********@n ewsreader2.netc ologne.de>,
Ph************* @gmx.de says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3915
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
8
4598
by: YAN | last post by:
Hi, I want to get the mac address from a machine, which i have the IP address of that machine, how can i do that? I know how to get the mac address of the local machine from the following code: Dim mc As System.Management.ManagementClass Dim mo As System.Management.ManagementObject mc = New System.Management.ManagementClass("Win32_NetworkAdapterConfiguration")
12
2467
by: johny smith | last post by:
I am trying to figure out a way to print the address of what called a certain function once inside the function. I am assuming that this information is on the stack somewhere. But can someone give me some advice? I don't know where to go to look this information up. Do I need to access the stack, and hence do some kind of inline assembly code to get the calling address?
2
1561
by: scott | last post by:
Lets say i have class A class B and class C. Class C inherites class B and class B inherites class A. Class A contains a vertual function (not pure) and class C contains the same vertual function. In my code i get the address of class A (i can't get the address of the other classes, its children) and then call the vertual function. It then in turn calls the vertual function of class C. Is there any way of getting the address of...
1
2299
by: Frederiek | last post by:
Hi, When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. Does that mean that the address of the data member is the same for every instance of that class? I would expect so, but I noticed something that makes me doubt a little.
8
3736
by: gilad | last post by:
hi, I would like to get the address of a class method in an instance. In the following sample the third like access the method. how can I get the address into a long variable - something like - long addr = B->*local_method ... (this does not work) class BCls { public: void method(void ) {
1
2361
by: Jamie J. Begin | last post by:
I'm very new to the world of Python and am trying to wrap my head around it's OOP model. Much of my OOP experience comes from VB.Net, which is very different. Let's say I wanted to create an object that simply outputted something like this: Developer Detroit Michigan
5
4032
by: ttoboobz | last post by:
Help anyone... I'm creating a simple address book java program that would store 100 records. The GUI should be able to accept the name, address, phone no., and email add of a person by clicking a button named ADD. It should also be able to EDIT or DELETE a record and can VIEW ALL ENTRIES. I have already INSTANTIATED the CLASS, and declared or created the necessary methods(accessor, mutator & constructors) and the arrays(container). I also...
4
1686
by: karen.homersham | last post by:
Just working through Apress Pro ASP NET.2.0.E Commerce in C Sharp. I am having problems compiling the following: using System; using System.Collections.Generic; using System.Text; namespace ECommerce.Common { public class Users
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.