public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Getting the byte offset of a member variable pointer type
@ 2005-06-22 13:48 Thomas Immich
  2005-06-22 15:55 ` Dave Korn
  0 siblings, 1 reply; 2+ messages in thread
From: Thomas Immich @ 2005-06-22 13:48 UTC (permalink / raw)
  To: gcc

Hi all,

I need to get the exact byte offset of a member variable inside a  
class in order to map a member variable pointer type to a concrete  
member variable instance. This worked fine until I ported my code to  
GCC 4.0, which is much stricter regarding type casts.

Unfortunately I cannot use the offsetof() macro, since I do not have  
enough information to do so.
The only information I have is:
     - The member variable type (in template parameter T, for example  
int)
     - A member variable pointer (m_memberVariablePtr, for example  
int MyClass::*)
     - The class type (in template parameter C, for example MyClass)
     - A concrete instance of type C (obj)

I want to get
     - The address of obj's concrete member variable (represented by  
m_memberVariablePtr)

Here is how I calculated this address in GCC 3.0:

const C* addressOfObject = static_cast<const C*>(&obj);
T* result = (T*)((std::size_t)addressOfObject + (size_t) 
m_memberVariablePtr);

However, this snippet does not compile in GCC 4.0, since I am not  
allowed to cast a member variable pointer to a size_t type. Is there  
any elegant possibility to get the address correctly?

Thanks in advance,

Thomas


^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: Getting the byte offset of a member variable pointer type
  2005-06-22 13:48 Getting the byte offset of a member variable pointer type Thomas Immich
@ 2005-06-22 15:55 ` Dave Korn
  0 siblings, 0 replies; 2+ messages in thread
From: Dave Korn @ 2005-06-22 15:55 UTC (permalink / raw)
  To: 'Thomas Immich', gcc

----Original Message----
>From: Thomas Immich
>Sent: 22 June 2005 14:49

> I need to get the exact byte offset of a member variable inside a
> class in order to map a member variable pointer type to a concrete
> member variable instance. This worked fine until I ported my code to
> GCC 4.0, which is much stricter regarding type casts.
 
  Well, you want to look in the RECORD_TYPE rtx for the class, then iterate
through the TYPE_FIELDS list by following the TREE_CHAIN members until you
find the relevant FIELD_DECL, at which point you can look up the
DECL_FIELD_BITPOS to get an INTEGER_CST holding the offset of the bit
position of the start of the field from the base of the struct.  Extract the
constant from the INTEGER_CST to a variable of type HOST_WIDEST_INT, divide
by BITS_PER_UNIT, and you're away.....

> Unfortunately I cannot use the offsetof() macro, since I do not have
> enough information to do so.

  Ah.  Maybe that wasn't the answer you really wanted?  This list is about
programming the internals of the compiler itself.  For general help with
using the compiler and programming, you really want to use the gcc-help
mailing list.  But, since I'm in a generous mood this afternoon:

> The only information I have is:
>      - The member variable type (in template parameter T, for example
> int)
>      - A member variable pointer (m_memberVariablePtr, for example
> int MyClass::*)
>      - The class type (in template parameter C, for example MyClass)
>      - A concrete instance of type C (obj)
> 
> I want to get
>      - The address of obj's concrete member variable (represented by
> m_memberVariablePtr)

> However, this snippet does not compile in GCC 4.0, since I am not
> allowed to cast a member variable pointer to a size_t type. Is there
> any elegant possibility to get the address correctly?

  So, you have an object 'obj' of class 'C' with a member var of type 'T',
and you have a pointer-to-member of type 'T C::*'?  And you want the address
of the actual variable?  Or the offset of it?

  Dereference the member pointer using the obj pointer.  That will give you
a plain reference to an object of type T.  Take the address of the
dereferenced member which will just be a plain T*.  So you want something
like

   &(obj->*m_memberVariablePtr)

to calculate the address.  Then cast both that value and 'obj' to unsigned
integers and subtract.

  This really is a very basic how-to-program-in-C++ question; *please* do
use the gcc-help list next time.

    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-06-22 15:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-22 13:48 Getting the byte offset of a member variable pointer type Thomas Immich
2005-06-22 15:55 ` Dave Korn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).