473,812 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)((struc t new *)&x.c - &x);

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

return EXIT_SUCCESS;
}

It always returns 0.

Thanks.

--

Alejo
Nov 13 '05
20 6492
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,m emb) ((size_t)&((typ e *)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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #12
In <eh************ *************** *****@4ax.com> Jack Klein <ja*******@spam cop.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,m emb) ((size_t)&((typ e *)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,m emb) ((size_t)&((typ e *)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,m emb) ((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,m emb) ((size_t)&((typ e *)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,m emb) ((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.hel sinki.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*******@spam cop.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,m emb) ((size_t)&((typ e *)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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #16
In <k4************ *************** *****@4ax.com> Jack Klein <ja*******@spam cop.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**********@no os.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**********@n oos.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

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

Similar topics

5
6348
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) The program is like below. class A {
9
2970
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 foo::* dataMember) :offsetof (foo, *dataMember) //Call this Line (A)
6
2205
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 following structure: struct weird { int x; /* sizeof(int)==4 here */
44
3762
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 doesn't make sense to me. The sticking point seems to be:
13
380
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) \ ((char *)&((type *)0)->mem - (char *)(type *)0)) Can anyone explain me how it works. 1)I can't understand what "0" casted to (type *) means
7
4540
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
5870
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
2435
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? If yes, then what if I have two or more structures. How can they reside at same address?.
2
3931
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 see how I should use pointers to members. Basically, I know how to get a member if I have an object and a pointer-to-member (obj.*ptr instead of obj + offset), but I don't know how to do the opposite: getting the object from a member address and...
0
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10404
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10417
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10139
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.