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

offsetof

Hello,

My implementation does not define offsetof, so I have designed a little
program that 'attempts' to find the relative position of a member in its
structure. It just does not work.

Could I get some pointers on what I am doing wrong (apart from being coding
so late at night).

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
struct new
{
int a;
char b;
char c;
} x;

size_t num;

num = (size_t)((struct new *)&x.c - &x);

printf( "%u\n", num );

return EXIT_SUCCESS;
}

It always returns 0.

Thanks.

--

Alejo
Nov 13 '05 #1
20 6393
Alejo wrote:
Hello,

My implementation does not define offsetof, so I have designed a little
program that 'attempts' to find the relative position of a member in its
structure. It just does not work.

Could I get some pointers on what I am doing wrong (apart from being coding
so late at night).

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
struct new
{
int a;
char b;
char c;
} x;

size_t num;

num = (size_t)((struct new *)&x.c - &x);

printf( "%u\n", num );

return EXIT_SUCCESS;
}

It always returns 0.


Leave that aside. Why are you doing this anyways? The offset of
members is not a terribly portable quantity. So if this has todo with
loading/saving to a file [or memory buffer] you ought to rethink your
design.

As to the general question, maybe .c is the first element as packed by
the compiler. Try doing

x.c = 4;

and get the assembler code the compiler produces. My compiler [gcc
3.3.1 pre-release] produces

main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movb $0, x+5
xorl %eax, %eax
leave
ret

Which seems to be at the end [not start].

Tom

Nov 13 '05 #2
Sorry, I already realised where the mistake is. I should hve done a cast
(char *) instead of (struct new *). Like:

num = (size_t)((char *)&x.c - (char *)&x);

Well, thanks anyway.

Alejo wrote:
Hello,

My implementation does not define offsetof, so I have designed a little
program that 'attempts' to find the relative position of a member in its
structure. It just does not work.

Could I get some pointers on what I am doing wrong (apart from being
coding so late at night).

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
struct new
{
int a;
char b;
char c;
} x;

size_t num;

num = (size_t)((struct new *)&x.c - &x);

printf( "%u\n", num );

return EXIT_SUCCESS;
}

It always returns 0.

Thanks.

--

Alejo


Nov 13 '05 #3
"Alejo" <al***@chello.no> wrote in message
news:fKGQa.16616$KF1.303303@amstwist00...
Hello,

My implementation does not define offsetof, so I have designed a little
program that 'attempts' to find the relative position of a member in its
structure. It just does not work.

Could I get some pointers on what I am doing wrong (apart from being coding so late at night).

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
struct new
{
int a;
char b;
char c;
} x;

size_t num;

num = (size_t)((struct new *)&x.c - &x);

printf( "%u\n", num );

return EXIT_SUCCESS;
}

It always returns 0.

Thanks.

--

Alejo


Hi,

Both(x.c and x) are interpreted as struct new *.
And the difference is less than one struct new: so 0.
This one is working:
num=(size_t)(&x.c-(char*)&x);

Marco
Nov 13 '05 #4
In article <fKGQa.16616$KF1.303303@amstwist00>, Alejo <al***@chello.no>
wrote:
Hello,
First, I will _not_ send emails, no matter how much you request them. I
will send emails to you if I intend you to read something that I don't
want the whole world to read, but if I post to the newsgroup then there
is no point in sending an email. You post here, you read here.
My implementation does not define offsetof, so I have designed a little
program that 'attempts' to find the relative position of a member in its
structure. It just does not work.

Could I get some pointers on what I am doing wrong (apart from being coding
so late at night).

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
struct new
{
int a;
char b;
char c;
} x;

size_t num;

num = (size_t)((struct new *)&x.c - &x);

printf( "%u\n", num );

return EXIT_SUCCESS;
}

It always returns 0.


Not suprisingly. If you had an array

struct new array [100];

how many bytes are &array [0] and &array [1] apart? What is the
difference (&array [1]) - (&array [0])? If two pointers p and q are x
byte apart, how does the compiler calculate p - q?
Nov 13 '05 #5
On Tue, 15 Jul 2003 01:09:28 +0200, Alejo <al***@chello.no> wrote in
comp.lang.c:
Hello,

My implementation does not define offsetof, so I have designed a little
program that 'attempts' to find the relative position of a member in its
structure. It just does not work.


Then you shouldn't be posting to comp.lang.c because whatever you
have, it is not a C compiler.

Or perhaps you have neglected to include <stddef.h>?

Understand that C provides for free-standing implementations, for
embedded systems and such, which are not required to provide even one
single function from the standard library, but they are still required
to provide the macros defined in <stddef.h>.

If what you have does not provide a working offsetof() macro in
<stddef.h>, it is literally not a C compiler.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #6
Jim
On Mon, 14 Jul 2003 23:16:15 GMT, Tom St Denis <to********@iahu.ca>
wrote:
Alejo wrote:
Hello,

My implementation does not define offsetof, so I have designed a little
program that 'attempts' to find the relative position of a member in its
structure. It just does not work.

Could I get some pointers on what I am doing wrong (apart from being coding
so late at night).

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
struct new
{
int a;
char b;
char c;
} x;

size_t num;

num = (size_t)((struct new *)&x.c - &x);

printf( "%u\n", num );

return EXIT_SUCCESS;
}

It always returns 0.


Leave that aside. Why are you doing this anyways? The offset of
members is not a terribly portable quantity. So if this has todo with
loading/saving to a file [or memory buffer] you ought to rethink your
design.

As to the general question, maybe .c is the first element as packed by
the compiler. Try doing

x.c = 4;

and get the assembler code the compiler produces. My compiler [gcc
3.3.1 pre-release] produces

main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movb $0, x+5
xorl %eax, %eax
leave
ret

Which seems to be at the end [not start].


If your compiler generates that for x.c = 4, then it is wrong.

Jim
Nov 13 '05 #7
Tom St Denis <to********@iahu.ca> wrote:
Alejo wrote:
struct new
{
int a;
char b;
char c;
} x;
As to the general question, maybe .c is the first element as packed by
the compiler.


Or maybe not, since it isn't allowed to shuffle elements this way. The
first element of a struct new in memory _must_ be a. There _cannot_ be
any padding before a. Hence, offsetof(struct new, a) must be 0, and
offsetof(struct new, c) is not allowed to be. Moreover, offsetof(struct
new, a) < offsetof(struct new, b) < offsetof(struct new, c).

Richard
Nov 13 '05 #8

On Tue, 15 Jul 2003, Jack Klein wrote:

On Tue, 15 Jul 2003 01:09:28 +0200, Alejo wrote in comp.lang.c:

My implementation does not define offsetof, so I have designed a little
program that 'attempts' to find the relative position of a member in its
structure. It just does not work.


Then you shouldn't be posting to comp.lang.c because whatever you
have, it is not a C compiler.


I thought offsetof() was new in C99. Am I mistaken?

-Arthur
Nov 13 '05 #9
Arthur J. O'Dwyer wrote:
I thought offsetof() was new in C99. Am I mistaken?


Yes. It's in ANSI C <stddef.h>.

--
Hallvard
Nov 13 '05 #10
On Tue, 15 Jul 2003 09:32:32 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@andrew.cmu.edu> wrote:
I thought offsetof() was new in C99. Am I mistaken?


It is in my copy of ANSI/ISO 9899-1990, copyright 1990. It is defined
in stddef.h.

Best wishes,

Bob
Nov 13 '05 #11
On Tue, 15 Jul 2003 16:27:38 +0200, Shill <no****@example.com> wrote
in comp.lang.c:
My implementation does not define offsetof [...]


#define offsetof(type,memb) ((size_t)&((type *)0)->memb)


Undefined behavior, dereferencing a null pointer.

The proper solution is to get a real C compiler.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #12
In <eh********************************@4ax.com> Jack Klein <ja*******@spamcop.net> writes:
On Tue, 15 Jul 2003 16:27:38 +0200, Shill <no****@example.com> wrote
in comp.lang.c:
> My implementation does not define offsetof [...]


#define offsetof(type,memb) ((size_t)&((type *)0)->memb)


Undefined behavior, dereferencing a null pointer.

The proper solution is to get a real C compiler.


All the real C compilers I have ever used define offsetof in a similar
way. There is nothing preventing a C compiler from making undefined
behaviour work.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #13
Jack Klein wrote:
On Tue, 15 Jul 2003 16:27:38 +0200, Shill <no****@example.com> wrote
in comp.lang.c:
> My implementation does not define offsetof [...]
#define offsetof(type,memb) ((size_t)&((type *)0)->memb)


Undefined behavior, dereferencing a null pointer.


Thats the way most libraries implement offsetof, it works because it is a
constant expression which will be calculated at compile time. Of cause, a
more logical way would be

#define offsetof(type,memb) ((char *)&((type *)0)->memb-(char *) 0)

because offsetof should returm an offset_t not a size_t (but that violates
the standard).
The proper solution is to get a real C compiler.


An ANSI-C compiler should be sufficient, offsetof is part of the standard
library.
--
Best Regards
Sven
Nov 13 '05 #14
Sven Gohlke,,, <sv**@clio.in-berlin.de> scribbled the following:
Jack Klein wrote:
On Tue, 15 Jul 2003 16:27:38 +0200, Shill <no****@example.com> wrote
in comp.lang.c:
> My implementation does not define offsetof [...]

#define offsetof(type,memb) ((size_t)&((type *)0)->memb)
Undefined behavior, dereferencing a null pointer. Thats the way most libraries implement offsetof, it works because it is a
constant expression which will be calculated at compile time. Of cause, a
more logical way would be #define offsetof(type,memb) ((char *)&((type *)0)->memb-(char *) 0) because offsetof should returm an offset_t not a size_t (but that violates
the standard).
I think you're missing something here. The ANSI C standard does not
specify that the expression &((type *)0)->memb should be calculated all
at compile time. The people who wrote those libraries are usually the
same ones who wrote the compilers, so they can benefit from the
knowledge that on *THEIR* compiler, it's evaluated all at compile time,
so it's safe. But this doesn't imply that it would be safe on any other
compiler.
The proper solution is to get a real C compiler.

An ANSI-C compiler should be sufficient, offsetof is part of the standard
library.


The behaviour of offsetof is standard, but any particular implementation
of it is not. Jack Klein's point was that any ANSI C compiler is
required to provide *an* implementation of offsetof, but there is no
particular implementation of offsetof that the compilers would be
required to choose.
In other words: Leave offsetof for the library to implement. If you try
implementing it yourself, you'll run into undefined behaviour.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I will never display my bum in public again."
- Homer Simpson
Nov 13 '05 #15
On 16 Jul 2003 10:07:16 GMT, Da*****@cern.ch (Dan Pop) wrote in
comp.lang.c:
In <eh********************************@4ax.com> Jack Klein <ja*******@spamcop.net> writes:
On Tue, 15 Jul 2003 16:27:38 +0200, Shill <no****@example.com> wrote
in comp.lang.c:
> My implementation does not define offsetof [...]

#define offsetof(type,memb) ((size_t)&((type *)0)->memb)


Undefined behavior, dereferencing a null pointer.

The proper solution is to get a real C compiler.


All the real C compilers I have ever used define offsetof in a similar
way. There is nothing preventing a C compiler from making undefined
behaviour work.

Dan


Of course not, in many instances a compiler is required to make
undefined behavior work. For example, how fopen() actually
establishes a connection to some type of physical device is undefined,
but a compiler makes it work.

One can't implement the entire standard C library without doing some
things that are undefined according to the standard. The
implementation and its library are not bound by the same constraints
as conforming programs.

The point is the OP says his compiler lacks a definition of the
offsetof() macro. Even free-standing implementations, which are not
required to provide even one single standard library function, are
required to provide the offsetof() and other macros in stddef.h, among
other things.

So either the OP is mistaken, or whatever it is he is using is not a C
compiler, in which case he's off-topic here.

The C language does not define an implementation that does not provide
a workable offsetof() macro in stddef.h.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #16
In <k4********************************@4ax.com> Jack Klein <ja*******@spamcop.net> writes:
The point is the OP says his compiler lacks a definition of the
offsetof() macro. Even free-standing implementations, which are not
required to provide even one single standard library function, are
required to provide the offsetof() and other macros in stddef.h, among
other things.

So either the OP is mistaken, or whatever it is he is using is not a C
compiler, in which case he's off-topic here.
If we adopt a rigid black and white position, then there is probably no C
compiler out there, they're complex enough programs to have bugs of one
kind of another.

Furthermore, this group is still dealing with pre-ANSI C issues and no
pre-ANSI C compiler can even hope to qualify as a conforming C compiler.
The C language does not define an implementation that does not provide
a workable offsetof() macro in stddef.h.


This doesn't prevent the existence of implementations that don't do that.
Either because they predate the C standard or because they did not
attempt to be conforming implementations (for one reason or another).

Therefore, questions about how to implement the functionality of offsetof
are topical in this newsgroup, even if the most portable solution invokes
undefined behaviour.

But I still suspect that the OP simply failed to include <stddef.h> and
expected offsetof to be available as some kind of compiler or preprocessor
built-in functionality.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #17
In 'comp.lang.c', "Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> wrote:
I thought offsetof() was new in C99. Am I mistaken?


Yes. It came with C90 in <stddef.h>

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #18
In <Xn***************************@130.133.1.4> Emmanuel Delahaye <em**********@noos.fr> writes:
In 'comp.lang.c', "Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> wrote:
I thought offsetof() was new in C99. Am I mistaken?


Yes. It came with C90 in <stddef.h>


Nope, it came with C89.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #19
>> Yes. It came with C90 in <stddef.h>

Nope, it came with C89.


http://gcc.gnu.org/onlinedocs/gcc-3....Standards.html

The original ANSI C standard (X3.159-1989) was ratified
in 1989 and published in 1990. This standard was ratified
as an ISO standard (ISO/IEC 9899:1990) later in 1990.
There were no technical differences between these
publications, although the sections of the ANSI standard
were renumbered and became clauses in the ISO standard.
This standard, in both its forms, is commonly known as C89,
or occasionally as C90, from the dates of ratification.

C89 and C90 are synonymous.

Nov 13 '05 #20
In <Xn***************************@130.133.1.4> Emmanuel Delahaye <em**********@noos.fr> writes:
In 'comp.lang.c', Da*****@cern.ch (Dan Pop) wrote:
Nope, it came with C89.


C89 is ANSI. It doesn't exist outside the USA ;-)


Does this mean that C90 ceased to exist outside the USA when it was
adopted as an ANSI standard? ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #21

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

Similar topics

5
by: Hiroki Horiuchi | last post by:
Hello. I wrote a program, but g++ warns a.c:11: warning: invalid access to non-static data member `A::y' of NULL object a.c:11: warning: (perhaps the `offsetof' macro was used incorrectly) ...
9
by: Exits Funnel | last post by:
Consider this code which is a very trimmed down version of some I've inherited and am trying to port from windows to g++: //Begin test1.cpp class foo { int i; int j; }; class bar { bar (int...
6
by: Arthur J. O'Dwyer | last post by:
As far as I know, C89/C90 did not contain the now-standard offsetof() macro. Did C89 mandate that structs had to have a consistent layout? For example, consider the typical layout of the...
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
13
by: luke | last post by:
hi all, i have another question. I've read the FAQ regarding my former question (sizeof struct and union) and in question 2.14 it talks about offset macro. #define offsetof(type, mem) ((size_t)...
7
by: Fred Zwarts | last post by:
Consider the following definition: typedef struct { int a; int b; } s; Now I have a function void f (int i) { ... }
8
by: Pawel | last post by:
Hallo group members. //p1.cpp #include <stdio.h> #include <linux/stddef.h> struct Person { int m_age; char* m_name; };
11
by: Kavya | last post by:
offsetof(T,m) (size_t)&(((T*)0)->m) Why do we always start from 0 in this macro to access the offset of structure or union. Does standard guarantees that structure and union reside at address 0?...
2
by: Imre | last post by:
Hi I know that offsetof is basically a C leftover, and only works for POD types, not classes, so it is recommended that I use pointers to members instead. However, I have a problem where I don't...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.