473,811 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

offsetof() macro

Hi,

Can somebody please help me grok the offsetof() macro?

I've found an explanation on
http://www.embedded.com/shared/print...cleID=18312031
but I'm afraid it still doesn't make sense to me.

The sticking point seems to be:

((s *)0) takes the integer zero and casts it as a pointer to s.

To my untrained eye that would basically result in a null pointer. Does
this expression result in some special behaviour or am I missing
something?

Thanks.

Simon

--
"Being a social outcast helps you stay concentrated on the really important
things, like thinking and hacking." - Eric S. Raymond

Nov 15 '05
44 3759
DevarajA <no@spam.com> writes:
Flash Gordon ha scritto:
DevarajA wrote:
Eric Laberge ha scritto:

<snip>
Both "NULL" and "offsetof" are defined in <stddef.h> (7.17 Common
de?nitions
<stddef.h>).

NULL is implementation-defined, but (6.3.2.3 Pointers) an integer
constant with value 0 is the same as NULL.

No, a constant expression in the *source* code used in a pointer
context is a null pointer constant. However, that has no bearing on
how a null pointer is represented in the program when it is run.
I suppose if, eg. the implementation decided that the value of NULL is
0x1234, then constant 0, when used as a pointer, would be equal to
0x1234.

No, NULL has to be and integer constant expression evaluating to 0
or such an expression cast to void*. However, if a null pointer is
not all bits 0 then the compiler will have to change it to whatever
representation is used for a null pointer when it compiles the code.


So, tell me if I have understood... null pointer constant needs not to
have an all-0 internal representation (it is implementation-defined),
but in the source code it must be represented as 0. And NULL macro
just expands into the null pointer constant, but I may also redefine
it. Am I right?


Close.

A "null pointer" and a "null pointer constant" are two different
things. A null pointer constant is a construct that can appear in C
source code. A null pointer is a pointer value that can occur during
execution time.

An integer constant 0 is a valid null pointer constant. If you assign
a null pointer constant to a pointer object, that object's value at
execution time will be a null pointer. The execution-time
representation of a null pointer may or may not be all-bits-zero. (If
a null pointer has a representation other than all-bits-zero, it's up
to the compiler to do whatever conversion might be necessary.)

The macro NULL expands to a null pointer constant. You cannot legally
redefine NULL yourself. Your compiler might let you get away with it,
but there's absolutely no reason to do so.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #11
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <jY************ *********@news4 .tin.it>,
DevarajA <no@spam.com> wrote:
So, tell me if I have understood... null pointer constant needs not to
have an all-0 internal representation (it is implementation-defined),


Right.
but in the source code it must be represented as 0.


The standard does not preclude other source-code representations , but 0
is the only portable one.


(See my other followup for a discussion of the distinction between
"null pointer constants" and "null pointers".)

The standard defined a "null pointer constant" as

An integer constant expression with the value 0, or such an
expression cast to type void *.

So the following are all valid null pointer constants under any
possible conforming implementation:

0
(void*)0
(1-1)
(void*)('-'-'-')
(void*)('/'/'/'-'/'/'/')

All but the first two are considered silly, but they're no less valid
for that.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #12
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
So the following are all valid null pointer constants under any
possible conforming implementation: 0
(void*)0
(1-1)
(void*)('-'-'-')
(void*)('/'/'/'-'/'/'/')
That's sick!

I like it ;-)
All but the first two are considered silly, but they're no less valid
for that.

--
"Who Leads?" / "The men who must... driven men, compelled men."
"Freak men."
"You're all freaks, sir. But you always have been freaks.
Life is a freak. That's its hope and glory." -- Alfred Bester, TSMD
Nov 15 '05 #13
Keith Thompson ha scritto:
DevarajA <no@spam.com> writes:
Flash Gordon ha scritto:
DevarajA wrote:
Eric Laberge ha scritto:

<snip>

>Both "NULL" and "offsetof" are defined in <stddef.h> (7.17 Common
>de?nitio ns
><stddef.h> ).
>
>NULL is implementation-defined, but (6.3.2.3 Pointers) an integer
>constant with value 0 is the same as NULL.

No, a constant expression in the *source* code used in a pointer
context is a null pointer constant. However, that has no bearing on
how a null pointer is represented in the program when it is run.
>I suppose if, eg. the implementation decided that the value of NULL is
>0x1234, then constant 0, when used as a pointer, would be equal to
>0x1234.

No, NULL has to be and integer constant expression evaluating to 0
or such an expression cast to void*. However, if a null pointer is
not all bits 0 then the compiler will have to change it to whatever
representati on is used for a null pointer when it compiles the code.


So, tell me if I have understood... null pointer constant needs not to
have an all-0 internal representation (it is implementation-defined),
but in the source code it must be represented as 0. And NULL macro
just expands into the null pointer constant, but I may also redefine
it. Am I right?

Close.

A "null pointer" and a "null pointer constant" are two different
things. A null pointer constant is a construct that can appear in C
source code. A null pointer is a pointer value that can occur during
execution time.

An integer constant 0 is a valid null pointer constant. If you assign
a null pointer constant to a pointer object, that object's value at
execution time will be a null pointer. The execution-time
representation of a null pointer may or may not be all-bits-zero. (If
a null pointer has a representation other than all-bits-zero, it's up
to the compiler to do whatever conversion might be necessary.)

The macro NULL expands to a null pointer constant. You cannot legally
redefine NULL yourself. Your compiler might let you get away with it,
but there's absolutely no reason to do so.


Then using NULL is the same as using 0? So why in C many people use
NULL? Is that to avoid portability problems (in the case of a weird
non-standard implementation that has the null ptr const !=0)?

--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167
http://counter.li.org
Nov 15 '05 #14
DevarajA <no@spam.com> writes:
Keith Thompson ha scritto:

[...]
A "null pointer" and a "null pointer constant" are two different
things. A null pointer constant is a construct that can appear in
C source code. A null pointer is a pointer value that can occur
during execution time. An integer constant 0 is a valid null
pointer constant. If you assign a null pointer constant to a
pointer object, that object's value at execution time will be a
null pointer. The execution-time representation of a null pointer
may or may not be all-bits-zero. (If a null pointer has a
representation other than all-bits-zero, it's up to the compiler to
do whatever conversion might be necessary.) The macro NULL expands
to a null pointer constant. You cannot legally redefine NULL
yourself. Your compiler might let you get away with it, but
there's absolutely no reason to do so.


Then using NULL is the same as using 0? So why in C many people use
NULL? Is that to avoid portability problems (in the case of a weird
non-standard implementation that has the null ptr const !=0)?


The advantage of using NULL is documentation. It makes it obvious
that what's intended is a pointer value, not an integer value.

For example:

a = 0;
b = 0.0;
c = '\0';
d = NULL;

You can tell even without looking at the declarations (assuming a sane
programmer) that a is an integer, b is a floating-point variable, c is
a character, and d is a pointer.

There are those who advocate using 0 rather than the NULL macro as a
null pointer constant. They are, of course, wrong, since they
disagree with me.

(If I were designing the language from scratch, there would be a
keyword, either "nil" or "null", that would be the *only* null pointer
constant. Using 0 in a pointer context would be a constraint error.
The current confusing situation is, I think, a remnant of the early
days of C when it was assumed that a null pointer *was* all-bits-zero,
and even non-zero integer constants were commonly used as pointers.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #15
>
There are those who advocate using 0 rather than the NULL macro as a
null pointer constant. They are, of course, wrong, since they
disagree with me.

(If I were designing the language from scratch, there would be a
keyword, either "nil" or "null", that would be the *only* null pointer
constant. Using 0 in a pointer context would be a constraint error.


What do you mean by "using 0 in a pointer context"? In another post you
have said "An integer constant 0 is a valid null pointer constant. If
you assign a null pointer constant to a pointer object, that object's
value at execution time will be a null pointer."+"The macro NULL expands
to a null pointer constant." And now you say that using 0 is a
constraint error. I think I haven't understood yet :-(

And on every compiler I've tried, int *pt=0; works without errors or
warnigs. And also the standard says that a constant expression
evaluating to 0 or such an expression cast to void*, assigned to a
pointer of wathever type makes it a null pointer.

Sorry if I'm wasting your time, but I'm a bit confused...

--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167
http://counter.li.org
Nov 15 '05 #16
DevarajA <no@spam.com> wrote:

[DevarajA: please preserve attribution lines, to make clear who wrote
what. Attribution restored.]
Keith Thompson wrote:
There are those who advocate using 0 rather than the NULL macro as a
null pointer constant. They are, of course, wrong, since they
disagree with me.
:-)
(If I were designing the language from scratch, there would be a
keyword, either "nil" or "null", that would be the *only* null pointer
constant. Using 0 in a pointer context would be a constraint error.


What do you mean by "using 0 in a pointer context"? In another post you
have said "An integer constant 0 is a valid null pointer constant. If
you assign a null pointer constant to a pointer object, that object's
value at execution time will be a null pointer."+"The macro NULL expands
to a null pointer constant." And now you say that using 0 is a
constraint error. I think I haven't understood yet :-(


Please read again: Keith _would_make_ using 0 in a pointer context a
constraint violation, _if_he_were_re-designing_the_l anguage_C_.

C-as-we-know-it: int *p = 0; /* correct, but bad style */
Keith's revamped C: int *p = 0; /* constraint violation */

(FWIW, I'd probably do the same.)

<snip>

Best regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.
Nov 15 '05 #17
Irrwahn Grausewitz ha scritto:
DevarajA <no@spam.com> wrote:

[DevarajA: please preserve attribution lines, to make clear who wrote
what. Attribution restored.]

Keith Thompson wrote:
There are those who advocate using 0 rather than the NULL macro as a
null pointer constant. They are, of course, wrong, since they
disagree with me.

:-)

(If I were designing the language from scratch, there would be a
keyword, either "nil" or "null", that would be the *only* null pointer
constant. Using 0 in a pointer context would be a constraint error.
What do you mean by "using 0 in a pointer context"? In another post you
have said "An integer constant 0 is a valid null pointer constant. If
you assign a null pointer constant to a pointer object, that object's
value at execution time will be a null pointer."+"The macro NULL expands
to a null pointer constant." And now you say that using 0 is a
constraint error. I think I haven't understood yet :-(

Please read again: Keith _would_make_ using 0 in a pointer context a
constraint violation, _if_he_were_re-designing_the_l anguage_C_.


Ok, got it :-) thank you all.
C-as-we-know-it: int *p = 0; /* correct, but bad style */
Keith's revamped C: int *p = 0; /* constraint violation */


Another thing.. looking into stddef.h I've seen that for C, NULL it is
defined as ((void*)0). The standard specifies that plain 0 is already a
null pointer constant. So why that cast? Maybe this is the last stupid
question for today :-)

--
Devaraja (Xdevaraja87^gm ail^c0mX)
Linux Registerd User #338167
http://counter.li.org
Nov 15 '05 #18
DevarajA <no@spam.com> writes:
There are those who advocate using 0 rather than the NULL macro as a
null pointer constant. They are, of course, wrong, since they
disagree with me.
(If I were designing the language from scratch, there would be a
keyword, either "nil" or "null", that would be the *only* null pointer
constant. Using 0 in a pointer context would be a constraint error.


What do you mean by "using 0 in a pointer context"? In another post
you have said "An integer constant 0 is a valid null pointer
constant. If you assign a null pointer constant to a pointer object,
that object's value at execution time will be a null pointer."+"The
macro NULL expands to a null pointer constant." And now you say that
using 0 is a constraint error. I think I haven't understood yet :-(


Look again. I said that if I were designing the language from
scratch, using 0 in a pointer context would be a constraint error (I
meant "constraint violation"). In C as it actually exists, 0 is a
valid null pointer constant, so it *can* be used in a pointer context.

For example:

int *p;
p = 0;

This is perfectly legal C. As a matter of style and clarity, I
prefer:

int *p;
p = NULL;

(In my hypothetical, non-existent C-like language, there would be a
keyword "nil" that would be the only legal null pointer constant.
"p = 0;" would be illegal, and there would be no need for the NULL
macro. I occasionally rant about how I'd redesign C if I had the
chance; don't take it too seriously.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #19
DevarajA <no@spam.com> wrote:
<snip>
Another thing.. looking into stddef.h I've seen that for C, NULL it is
defined as ((void*)0).
Depends on the implementation, yours happen to use the above
definition, another might define NULL as (0) or even
((void *)('9'-'4'-'5')).
The standard specifies that plain 0 is already a
null pointer constant. So why that cast?
I'm just guessing now; maybe the implementer thought: while we all
know that it's perfectly legitimate to implicitly convert a constant
integer expression with value 0 to any type of pointer, yielding a
null pointer of that type, I'll take a step further and _explicitly_
spell it out as ((void *)0) in the definition of the NULL macro.

Like saying: "Yes, I _really_ _really_ mean null pointer constant."

For a definite answer you'd have to ask the guy himself. ;o)
Maybe this is the last stupid
question for today :-)


There are no stupid questions, only stupid answers. :-)

Best regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.
Nov 15 '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 */
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?.
24
3503
by: Francine.Neary | last post by:
Just out of personal curiosity :) What do people use offsetof() for? I mean, I can understand why you'd want to be able to take the address of a member of a struct, but you can do that with just &(s.a) or similar. Why you'd care about the offset (which surely depends on how the compiler chooses to lay the struct out in memory), I don't really know. And if you did really care, won't offset(s,a) just be &(s.a) - &s ?
5
1902
by: mihirtr | last post by:
Hi, I have following structures and union typedef struct { int a; int b; char *c; }TEST_1;
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10653
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...
1
10408
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
10137
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...
1
7674
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6895
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
5564
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4352
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.