 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Sekhar Guest
|
Posted: Thu Jun 29, 2006 9:10 am Post subject: Offseting in Structures |
|
|
While going through one discussion forum i come across one interesting
query.
Without creating an object how to find the offset of a structure member
variable.
Something like
struct A
{
double i;
int j;
}
OFFSETOF(A, j) should return offset of member variable j.
I come across one standard function in stddef.h where this is already
defined as
#define offsetof(s,m) (size_t)&(((s *)0)->m)
and is working fine and the above snippet is giving me 8 on a VC++ 6.0
compiler
Can anyone shed some light of how this is working.
Thanks and Regards
Sujil C |
|
| Back to top |
|
 |
Jim Langston Guest
|
Posted: Thu Jun 29, 2006 9:10 am Post subject: Re: Offseting in Structures |
|
|
"Alf P. Steinbach" <alfps (AT) start (DOT) no> wrote in message
news:4ghhd9F1kkov1U1 (AT) individual (DOT) net...
| Quote: | * Sekhar:
While going through one discussion forum i come across one interesting
query.
Without creating an object how to find the offset of a structure member
variable.
Something like
struct A
{
double i;
int j;
}
OFFSETOF(A, j) should return offset of member variable j.
I come across one standard function in stddef.h where this is already
defined as
#define offsetof(s,m) (size_t)&(((s *)0)->m)
and is working fine and the above snippet is giving me 8 on a VC++ 6.0
compiler
Can anyone shed some light of how this is working.
It's compiler specific, relying on knowledge that with this compiler a
nullpointer corresponds to address 0, and that with this compiler it can
be dereferenced.
The standard library /implementation/ can use such compiler-specific
features, and this implementation does.
It won't necessarily work with other compilers.
|
I don't neccessarily see where it counts a null pointer. It seems the same
result would be concluded by:
A* ap = 0;
size_t offset = (size_t) &ap->m; |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Thu Jun 29, 2006 9:10 am Post subject: Re: Offseting in Structures |
|
|
* Sekhar:
| Quote: | While going through one discussion forum i come across one interesting
query.
Without creating an object how to find the offset of a structure member
variable.
Something like
struct A
{
double i;
int j;
}
OFFSETOF(A, j) should return offset of member variable j.
I come across one standard function in stddef.h where this is already
defined as
#define offsetof(s,m) (size_t)&(((s *)0)->m)
and is working fine and the above snippet is giving me 8 on a VC++ 6.0
compiler
Can anyone shed some light of how this is working.
|
It's compiler specific, relying on knowledge that with this compiler a
nullpointer corresponds to address 0, and that with this compiler it can
be dereferenced.
The standard library /implementation/ can use such compiler-specific
features, and this implementation does.
It won't necessarily work with other compilers.
--
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? |
|
| Back to top |
|
 |
Stuart Redmann Guest
|
Posted: Fri Jun 30, 2006 9:10 am Post subject: Re: Offseting in Structures |
|
|
BigBrian wrote:
| Quote: | Stuart Redmann wrote:
Alf is supposedly a mathematician, because his answer is
(a) completely right, and
(b) utterly useless.
IMHO, Alf's answer wasn't "utterly useless". This is a statement of
your opinion, not absolute fact.
-Brian
|
Well, I thought people would spot that this comment was meant
sarcastically. As an ex-student of theoretical computer science I have
deep respect of all mathematicians (including my wife), so I hope Alf
won't hold a grudge against me. The original joke goes like this:
A man climbs a mountain and gets lost. By chance there is a balloon
flying by, so he shouts 'Do you know where you are?' After some time he
gets the reply 'In a balloon.' How do we know that the balloon driver is
a mathematician?
Well, there are three indicators:
(a) his answer is well thought about,
(b) his answer is completey right, and
(c) his answer is utterly useless.
Regards,
Stuart |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|