473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Offset using pointer to member


Hello all,

For purposes of understanding the language better, I tried to determine the
offset of a struct member by using pointers to members (instead of something
such as the offsetof macro). However, if I understand correctly, this may
not be done portably. If I try to print a pointer to member, it just
converts to bool and I always get 1 (unless it's a null pointer to member).
There are no conversions from pointer to member to an integral type, so I
think I'm out of luck. Of course, I could hack the underlying
representation on my platform, but I want to be Standard-compliant...

Does anybody know of a way I can do what I want, or is my analysis correct
that it cannot be done portably?

Thanks,
Dave
Jul 22 '05 #1
4 4604
Dave wrote:
Hello all,

For purposes of understanding the language better, I tried to determine the
offset of a struct member by using pointers to members (instead of something
such as the offsetof macro). However, if I understand correctly, this may
not be done portably. If I try to print a pointer to member, it just
converts to bool and I always get 1 (unless it's a null pointer to member).
There are no conversions from pointer to member to an integral type, so I
think I'm out of luck. Of course, I could hack the underlying
representation on my platform, but I want to be Standard-compliant...

Does anybody know of a way I can do what I want, or is my analysis correct
that it cannot be done portably?

It's ugly but this should work.
template <typename T, typename V>
inline int MemberOffsetOf( V T::* v )
{
return ((int)&((( A* )0)->*v));
}

As for a compile-time version, you can't do it because you need to use
the "&" operator (address of) and this is apparently illegal in a
constant expression.

This is somthing like what it would look like for a compile-time
constant had the "&" operator been allowed.
template <typename T, typename V>
struct ClassValue
{
typedef T classtype;
typedef V valuetype;
template < V T::* P >
struct DoOffset
{
// & illegal here - sniff sniff ...
static const int offset = ( (int) & ( ( (T*) 0)->*P ) );
};
template <V T::* P>
char ( & SizeofThing() )[ DoOffset<P>::offset ];
};


template <typename T, typename V>
ClassValue< T, V > SizeofHelper( V T::* );
#define MemberOffsetOf( Member ) \
sizeof( SizeofHelper( Member ).SizeofThing< Member >() )
Jul 22 '05 #2
> It's ugly but this should work.

template <typename T, typename V>
inline int MemberOffsetOf( V T::* v )
{
return ((int)&((( A* )0)->*v));
}


Emphasis on _should_ here -- even though you and I know it's really
only doing compile-time math, this is still officially dereferencing a
null pointer, so officially it gives undefined behavior.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #3

"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
It's ugly but this should work.

template <typename T, typename V>
inline int MemberOffsetOf( V T::* v )
{
return ((int)&((( A* )0)->*v));
}


Emphasis on _should_ here -- even though you and I know it's really
only doing compile-time math, this is still officially dereferencing a
null pointer, so officially it gives undefined behavior.

--
Later,
Jerry.

The universe is a figment of its own imagination.


Good point here! But interestingly, that same comment would also be true of
the way the C macro offsetof is typically implemented!
Jul 22 '05 #4
> Good point here! But interestingly, that same comment would also be
true of
the way the C macro offsetof is typically implemented!


That's true, but a bit different -- it's part of the implementation,
and the implementor is free to do things that only work on that
particular implementation.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #5

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

Similar topics

4
by: Gactimus | last post by:
Here is a program that encodes and decodes a text file. What I need to do is write a C++ program that requests 3 different file names. One filename is for the source file to be encoded, another is...
0
by: D. Dante Lorenso | last post by:
I need to know that original number of rows that WOULD have been returned by a SELECT statement if the LIMIT / OFFSET where not present in the statement. Is there a way to get this data from PG ?...
11
by: Bradford Chamberlain | last post by:
I work a lot with multidimensional arrays of dynamic size in C, implementing them using a single-dimensional C array and the appropriate multipliers and offsets to index into it appropriately. I...
3
by: Arto Huusko | last post by:
Hello, I'm wondering about the portability and correctness of the following scenario. I have a generic doubly linked list structure, and it contains generic nodes that look like this: ...
15
by: junky_fellow | last post by:
I am trying to find the offset of a member "mbr" inside a structure "str" as follows: offset = &(struct str *)0->mbr; But, on compilation I get the following error: cc: Error: m1.c, line 55:...
10
by: junky_fellow | last post by:
I am trying to print the offset of a particulat member in a structure, but it's not working. I am using the following expression to print the offset. &(struct my_struct *)0->member_name ...
1
by: Adam Benson | last post by:
Hi, In C++ you could define a member offset pointer which worked on any instance of a class. i.e. class MyClass { int IntOne; int IntTwo;
9
by: John Goche | last post by:
Hello, Consider the following macro to get the memory offset of a class data member: #define OFFSET(CLASSNAME, MEMBER) ((int) (&((CLASSNAME *) 0)->MEMBER)) Given that 0 may not be the...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.