473,396 Members | 1,929 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,396 software developers and data experts.

finding offset of a class member at compile time

Hi,

Is there a way to find the offset of a class member at compile time.
e.g. class A{ int i; int j; char c; };

Here the offset of c = 8 bytes from the start of an object of A
(assuming 4 byte int). Can it be done at compile time.

Thanks in advance

Nov 14 '06 #1
19 8673
Geo

Rahul wrote:
Hi,

Is there a way to find the offset of a class member at compile time.
e.g. class A{ int i; int j; char c; };

Here the offset of c = 8 bytes from the start of an object of A
(assuming 4 byte int). Can it be done at compile time.

Thanks in advance
Have you tried offsetof() ?

Nov 14 '06 #2


On Nov 14, 6:31 pm, "Geo" <g...@remm.orgwrote:
Rahul wrote:
Hi,
Is there a way to find the offset of a class member at compile time.
e.g. class A{ int i; int j; char c; };
Here the offset of c = 8 bytes from the start of an object of A
(assuming 4 byte int). Can it be done at compile time.
Thanks in advanceHave you tried offsetof() ?

offset of will do ((A*)0)->i which will happen at run time (not
compile time). But i want to know it at compile time only.

something like &A::i would have been better, but that also dosn't seem
to rum at compile time.

Nov 14 '06 #3

Rahul skrev:
On Nov 14, 6:31 pm, "Geo" <g...@remm.orgwrote:
Rahul wrote:
Hi,
Is there a way to find the offset of a class member at compile time.
e.g. class A{ int i; int j; char c; };
Here the offset of c = 8 bytes from the start of an object of A
(assuming 4 byte int). Can it be done at compile time.
Thanks in advanceHave you tried offsetof() ?


offset of will do ((A*)0)->i which will happen at run time (not
compile time). But i want to know it at compile time only.
How offsetof does its stuff is an implementation detail, but it will
always be a compile-time result. Your problem is another as offsetof is
defined for POD types only. But why do you want to know such stuff in
the first place? Answering that question might give way to the "real"
solution.

/Peter
>
something like &A::i would have been better, but that also dosn't seem
to rum at compile time.
Nov 14 '06 #4
offset of is not giving the result at compile time.

The class is a POD, which has lots of members, the problem is that the
members have to be in a order (at least some of them, well its very
cryptic, but thats how the code has been written)

So to ensure that if somebody changes the class, he dosn't mess up the
order.
I m using a compile time macro, which creates an array of size (offset
(last_member-first_member)) to ensure that last_member is indeed
defined in the last.

Nov 14 '06 #5

Rahul wrote:
offset of is not giving the result at compile time.

The class is a POD, which has lots of members, the problem is that the
members have to be in a order (at least some of them, well its very
cryptic, but thats how the code has been written)

So to ensure that if somebody changes the class, he dosn't mess up the
order.
I m using a compile time macro, which creates an array of size (offset
(last_member-first_member)) to ensure that last_member is indeed
defined in the last.
Probably not the best solution But I think something like the may work

struct {
struct {
struct {
int a;
}A;
int b;
}B;
double c
}C:
char d;
};

Nov 14 '06 #6

Rahul wrote:
offset of is not giving the result at compile time.

The class is a POD, which has lots of members, the problem is that the
members have to be in a order (at least some of them, well its very
cryptic, but thats how the code has been written)

So to ensure that if somebody changes the class, he dosn't mess up the
order.
I m using a compile time macro, which creates an array of size (offset
(last_member-first_member)) to ensure that last_member is indeed
defined in the last.
Probably not the best solution But I think something like the may work

struct {
struct {
struct {
int a;
}A;
int b;
}B;
double c
}C:
char d;
}D;

Nov 14 '06 #7

Rahul wrote:
offset of is not giving the result at compile time.

The class is a POD, which has lots of members, the problem is that the
members have to be in a order (at least some of them, well its very
cryptic, but thats how the code has been written)

So to ensure that if somebody changes the class, he dosn't mess up the
order.
I m using a compile time macro, which creates an array of size (offset
(last_member-first_member)) to ensure that last_member is indeed
defined in the last.
Probably not the best solution But I think something like the may work

struct {
struct {
struct {
int a;
}A;
int b;
}B;
double c
}C:
char d;
}D;

Nov 14 '06 #8
I have NO idea why that apeared 3 times !!

Nov 14 '06 #9

Rahul skrev:
offset of is not giving the result at compile time.
In that case your C++ compiler is nonconforming. To be nonconforming in
this area is somewhat surprising, so I do believe you are wrong here.

NB. an expression such as size_t(&((A*)0)->i)) is a compile time
expression.

/Peter
[snip]

Nov 14 '06 #10
I am using gcc version 3.4.5.
I don;t know if it confirms to the standard or not.

But ((A*)0)-either using offsetof or explicitly is happening at run
time only, otherwise my COMPILE_TIME_ASSERT would have failed because
of -ve size array definition.

But at run time its printing the difference correctly (when i am doing
(first_member-last_member) and vice versa also.

Nov 14 '06 #11
* Rahul:
I am using gcc version 3.4.5.
I don;t know if it confirms to the standard or not.

But ((A*)0)-either using offsetof or explicitly is happening at run
time only, otherwise my COMPILE_TIME_ASSERT would have failed because
of -ve size array definition.

But at run time its printing the difference correctly (when i am doing
(first_member-last_member) and vice versa also.
The bugs are in your code.

Read the FAQ on how to post a question about code that doesn't work
correctly.

Then do as the FAQ says.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '06 #12

Rahul wrote:
Hi,

Is there a way to find the offset of a class member at compile time.
e.g. class A{ int i; int j; char c; };

Here the offset of c = 8 bytes from the start of an object of A
(assuming 4 byte int). Can it be done at compile time.

Thanks in advance
How about this, it works for me. I employ typelists, you just need to
put a typelist in your class, its not ver intrusive because the typedef
is a compile time object.

#include<iostream>
#include<cmath>
using namespace std;

struct null_type{};

template<class TYPE1,class TYPE2>
struct TypeList{
typedef TYPE1 Head;
typedef TYPE2 Tail;
};
template<class TYPE,unsigned long i>
struct At {
typedef typename At<typename TYPE::Tail,i-1>::type type;

};

template<class TYPE>
struct At<TYPE,0{
typedef typename TYPE::Head type;

};
template <class TLIST ,unsigned long i>
struct GetSize{
enum {value = sizeof(typename At<TLIST,i>::type) +
GetSize<TLIST,i-1>::value};
};

template <class TLIST>
struct GetSize<TLIST,0{
enum {value = sizeof(typename At<TLIST,0>::type)};
};

#define TYPELIST_1(type) TypeList<type,null_type>
#define TYPELIST_2(type1,type2) TypeList<type1,TYPELIST_1(type2) >
#define TYPELIST_3(type1,type2,type3)
TypeList<type1,TYPELIST_2(type2,type3) >
#define TYPELIST_4(type1,type2,type3,type4)
TypeList<type1,TYPELIST_3(type2,type3,type4) >

struct A {
typedef TYPELIST_4(int,double,char,unsigned long) theTypeList;
int i;
double x;
char ch;
unsigned long ul;
};
int main() {

cout << GetSize<A::theTypeList,2>::value;

}

Nov 14 '06 #13
Rahul:
Hi,

Is there a way to find the offset of a class member at compile time.
e.g. class A{ int i; int j; char c; };

Here the offset of c = 8 bytes from the start of an object of A
(assuming 4 byte int). Can it be done at compile time.

Thanks in advance

If you're working with a POD, then simply use the "offsetof" macro. If it
doesn't yield a compile-time constant, then you have a K++ compiler.

If working with a non-POD, then I think the only portable method would be to
create an object of it, e.g.:

SomeType obj;

size_t i = (char*)&obj.c - (char*)&obj;

, but this won't give you a compile-time constant.

--

Frederick Gotham
Nov 14 '06 #14

"Rahul" <ra*********@lucent.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>I am using gcc version 3.4.5.
I don;t know if it confirms to the standard or not.

But ((A*)0)-either using offsetof or explicitly is happening at run
time only, otherwise my COMPILE_TIME_ASSERT would have failed because
of -ve size array definition.
Sounds like your COMPILE_TIME_ASSERT isn't written correctly. But you
haven't shown that code, so that's just a guess. (And I have _no_ idea what
the heck you mean by "-ve size array definition"!)
But at run time its printing the difference correctly (when i am doing
(first_member-last_member) and vice versa also.
Show the code!

-Howard

Nov 14 '06 #15
Its this,

#define COMPILE_TIME_ASSERT(expr) do \
{ \
int temp[(expr) != 0 ? 1 : -1]; \
(void)(temp);\
} while(0)

So the following will casuse a compile time error.
COMPILE_TIME_ASSERT(2-2);

Nov 15 '06 #16
Le 15.11.2006 05:36, :
Its this,

#define COMPILE_TIME_ASSERT(expr) do \
{ \
int temp[(expr) != 0 ? 1 : -1]; \
(void)(temp);\
} while(0)

So the following will casuse a compile time error.
COMPILE_TIME_ASSERT(2-2);
Try this one:

#define CAT__(A,B) A ## B
#define CAT_(A,B) CAT__(A,B)
#define COMPILE_TIME_ASSERT(expr) \
typedef int CAT_(CTA_DummyType,__LINE__)[(expr) ? 1 : -1]

Note that the first CAT__ has two underscores and the second only one.

--
___________
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Pour bien répondre avec Google, ne pas cliquer
-'(__) « Répondre », mais « Afficher les options »,
_/___(_) puis cliquer « Répondre » (parmi les options).
Nov 15 '06 #17
Serge Paccalin wrote:
Le 15.11.2006 05:36, :
>Its this,

#define COMPILE_TIME_ASSERT(expr) do \
{ \
int temp[(expr) != 0 ? 1 : -1]; \
(void)(temp);\
} while(0)

So the following will casuse a compile time error.
COMPILE_TIME_ASSERT(2-2);

Try this one:

#define CAT__(A,B) A ## B
#define CAT_(A,B) CAT__(A,B)
#define COMPILE_TIME_ASSERT(expr) \
typedef int CAT_(CTA_DummyType,__LINE__)[(expr) ? 1 : -1]

Note that the first CAT__ has two underscores and the second only one.
That's unkosher. Any identifier with two consecutive underscores is
reserved to the implementation. Use CAT1_ instead.
Nov 15 '06 #18

"Rahul" <ra*********@lucent.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Its this,

#define COMPILE_TIME_ASSERT(expr) do \
{ \
int temp[(expr) != 0 ? 1 : -1]; \
(void)(temp);\
} while(0)

So the following will casuse a compile time error.
COMPILE_TIME_ASSERT(2-2);
(It would help if you'd follow the newsgroup guidelines, and quote what
you're responding to.)

How are you using this macro? You haven't shown the code using this, or the
structure you're using it on. You should always post _complete_ information
here, or else we're left groping in the dark.

Why are you using this declaration?:
int temp[(expr) != 0 ? 1 : -1];

Your code appears to check for inequality, as if all you want is for the two
numbers to be different. I thought you wanted to be sure the _order_ of the
members was correct. How are you doing that? What good does it do to only
accept _unequal_ values? (Hint: show the code!)

Anyway...

I did the following test, and it correctly tells me whether two members are
in the expected order or not:

struct TS1
{
// ok, correct order (a before b)
int a;
char b;
};

struct TS2
{
// not ok, wrong order (a after b)
char b;
int a;
};

#define COMPILE_TIME_ASSERT2(expr) do \
{ \
int temp[(expr)]; \
(void)(temp);\
} while(0)

int main()
{
COMPILE_TIME_ASSERT2(offsetof(TS1,b)-offsetof(TS1,a)); // ok
COMPILE_TIME_ASSERT2(offsetof(TS2,b)-offsetof(TS2,a)); // not ok

// ...
}

That code accepts TS1 fine, but for TS2 it reports that I can't have an
array larger than ffffffff bytes. So there's apparently a signed/unsigned
issue here. If I change the array definition like this:

int temp[(long)(expr)]; \

then I get the expected "negative subscript" error for TS2.

So, I think I've shown that offsetof is (at least with my compiler) a
compile-time value, eh? Try my code on your compiler. If it still fails
(by not causing an error), then you've got a non-compliant compiler.

-Howard

Nov 15 '06 #19
Le 15.11.2006 16:44, :
>#define CAT__(A,B) A ## B
That's unkosher. Any identifier with two consecutive underscores is
reserved to the implementation. Use CAT1_ instead.
OK, I didn't know that. I thought that only identifiers starting with
underscores (like __CAT or _EMAIL) were a problem.

--
___________
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Pour bien répondre avec Google, ne pas cliquer
-'(__) « Répondre », mais « Afficher les options »,
_/___(_) puis cliquer « Répondre » (parmi les options).
Nov 15 '06 #20

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

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
2
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...
4
by: Dave | last post by:
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). ...
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:...
4
by: __jakal__ | last post by:
Hello, I need to find out the time difference between UTC and local time. I am doing it the following way #include <sys/timeb.h> #include <stdio.h> int main() { struct timeb tp; ftime(&tp);
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...
17
by: Amchi | last post by:
Alright .... this makes no sense ... Declared a class 'diskStorage' in a header ...diskStorage.h Defined it's contructor and methods ... in diskStorage.cpp Included diskStorage header in...
17
by: abhimanyu.v | last post by:
Hi Guys, I have one doubt. The test program is given below. It uses two way of finding out the offset of a variable in structure. I executed the program and found the same result. My question...
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.