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

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

Jun 29 '06 #1
8 1128
* 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.

--
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?
Jun 29 '06 #2

"Alf P. Steinbach" <al***@start.no> wrote in message
news:4g*************@individual.net...
* 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;

Jun 29 '06 #3
* Jim Langston:
"Alf P. Steinbach" <al***@start.no> wrote in message
news:4g*************@individual.net...
* 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.


Huh?
It seems the same
result would be concluded by:

A* ap = 0;
size_t offset = (size_t) &ap->m;


Yes.

But note that if a nullpointer is actually address 0x0FFFFFFF, or
something, then this will/may produce something very much different than
the offset.

That's where the nullpointer representation enters the picture; the
ability to dereference a known nullpointer in this context is also
crucial, although that's something I think one can rely on with every
existing C++ compiler.
--
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?
Jun 29 '06 #4

Alf P. Steinbach wrote:
....
Yes.

But note that if a nullpointer is actually address 0x0FFFFFFF, or
something,...


Strictly speaking, you are correct. However I have never ever seen
null implemented as somthing other than 0 and I suspect I never will.
So practically speaking, it is irrelevant.

Jun 29 '06 #5
Alf P. Steinbach wrote:
* 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.


Alf is supposedly a mathematician, because his answer is
(a) completely right, and
(b) utterly useless.

I think what you want to know is how the expression
"(size_t)&(((s *)0)->m)"
works, assuming that the null pointer representation of your compiler is
0x0. Let's interpret this expression one step at a time.

"(s *)0" denotes a pointer of type s that points to the memory location
0x0 (this is the compiler specific part, since it may point to any
location your compiler vendor can think of).

"((s *)0)->m" is the member m of the null pointer. Since the null
pointer points to the physical address 0x0, this expression effectively
is the offset of the member m of an object of class s.

"&(((s *)0)->m)" takes the address of the member m of the null pointer.
Note that the expression "((s *)0)->m" is not the address of the member
m but the _value_ of m. Nevertheless the expression "((s *)0)->m" is
also a lvalue, thus the address of the expression can be retrieved.

"(size_t)&(((s *)0)->m)" simply casts the address of member m of the
null pointer to the type size_t.

The expression is not platform independent, but can be made so quite
easily. I leave it as a interesting task for you to try.

Regards,
Stuart


Jun 29 '06 #6

Stuart Redmann wrote:
Alf P. Steinbach wrote:
* 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.


Alf is supposedly a mathematician, because his answer is
(a) completely right, and
(b) utterly useless.

I think what you want to know is how the expression
"(size_t)&(((s *)0)->m)"
works, assuming that the null pointer representation of your compiler is
0x0. Let's interpret this expression one step at a time.

"(s *)0" denotes a pointer of type s that points to the memory location
0x0 (this is the compiler specific part, since it may point to any
location your compiler vendor can think of).

"((s *)0)->m" is the member m of the null pointer. Since the null
pointer points to the physical address 0x0, this expression effectively
is the offset of the member m of an object of class s.

"&(((s *)0)->m)" takes the address of the member m of the null pointer.
Note that the expression "((s *)0)->m" is not the address of the member
m but the _value_ of m. Nevertheless the expression "((s *)0)->m" is
also a lvalue, thus the address of the expression can be retrieved.

"(size_t)&(((s *)0)->m)" simply casts the address of member m of the
null pointer to the type size_t.

The expression is not platform independent, but can be made so quite
easily. I leave it as a interesting task for you to try.

Regards,
Stuart

Thanks Stuart for giving me exactly what i want

Jun 29 '06 #7

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

Jun 29 '06 #8
BigBrian wrote:
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
Jun 30 '06 #9

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

Similar topics

1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
33
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
6
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
14
by: pmclinn | last post by:
I've noticed that many programmers use classes to store data about such things like: Class Customers .....Phone ....ID ....Address End Class....
2
by: thomasfarrow | last post by:
At work, our development team has a development standards document that insists Structures should never be used. I'm looking to change this standard but need a suitable argument in order to make...
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
8
by: Bob Altman | last post by:
Hi all, I have a structure that includes a constructor. I want to add a bunch of these structures to an STL map (whose index is an int). If I define the map like this: map<int,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.