From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11411 invoked by alias); 22 Jun 2005 15:55:13 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 11380 invoked by uid 22791); 22 Jun 2005 15:55:10 -0000 Received: from host217-40-213-68.in-addr.btopenworld.com (HELO SERRANO.CAM.ARTIMI.COM) (217.40.213.68) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Wed, 22 Jun 2005 15:55:10 +0000 Received: from mace ([192.168.1.25]) by SERRANO.CAM.ARTIMI.COM with Microsoft SMTPSVC(6.0.3790.211); Wed, 22 Jun 2005 16:55:13 +0100 From: "Dave Korn" To: "'Thomas Immich'" , Subject: RE: Getting the byte offset of a member variable pointer type Date: Wed, 22 Jun 2005 15:55:00 -0000 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit In-Reply-To: <311FCBFD-7462-42C9-8701-5869B3AC37BE@ergosign.de> Message-ID: X-SW-Source: 2005-06/txt/msg00971.txt.bz2 ----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....