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

confusing macro

#define FOO(x,y) ((int)&(((x*)NULL)->y))

How is this legal? It is accessing the member of a struct whose
address is "zero". Since NULL is zero.
Nov 13 '05 #1
5 1843
Mantorok Redgormor <ne*****@tokyo.com> scribbled the following:
#define FOO(x,y) ((int)&(((x*)NULL)->y)) How is this legal? It is accessing the member of a struct whose
address is "zero". Since NULL is zero.


It depends. On some implementations, & and -> cancel each other out,
leaving essentially a pointer substraction. On others, they don't. The
moral of the story is: The implementation writer is allowed to define
such a macro. You are not.
("You" meaning every common programmer, not just Mantorok.)

--
/-- 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 wish someone we knew would die so we could leave them flowers."
- A 6-year-old girl, upon seeing flowers in a cemetery
Nov 13 '05 #2
In <41*************************@posting.google.com> ne*****@tokyo.com (Mantorok Redgormor) writes:
#define FOO(x,y) ((int)&(((x*)NULL)->y))

How is this legal? It is accessing the member of a struct whose
address is "zero". Since NULL is zero.


It's not legal, but it works because it doesn't actually access the
member, it merely takes its address. If you replace int by size_t, you
get the typical implementation of the offsetof macro from <stddef.h>.

Strictly speaking, it is invoking undefined behaviour, but I have yet
to see a real world implementation where it doesn't work as intended.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3
ne*****@tokyo.com (Mantorok Redgormor) writes:
#define FOO(x,y) ((int)&(((x*)NULL)->y))

How is this legal? It is accessing the member of a struct whose
address is "zero". Since NULL is zero.


It's not. However, you probably pulled this from a system header
(defining offsetof(), no doubt). System headers are allowed to make
all sorts of assumptions about the implementation, and even invoke
behavior which is undefined by the Standard, so long as they no that
the behavior is "defined" for their particular implementation.

The moral of the story? Don't write code like the above (unless your
an Implementor), but don't trip if you come across it in a system
header.

-Micah
Nov 13 '05 #4
Da*****@cern.ch (Dan Pop) wrote in message news:<bk**********@sunnews.cern.ch>...
In <41*************************@posting.google.com> ne*****@tokyo.com (Mantorok Redgormor) writes:
#define FOO(x,y) ((int)&(((x*)NULL)->y))

How is this legal? It is accessing the member of a struct whose
address is "zero". Since NULL is zero.
It's not legal, but it works because it doesn't actually access the
member, it merely takes its address. If you replace int by size_t, you
get the typical implementation of the offsetof macro from <stddef.h>.

Strictly speaking, it is invoking undefined behaviour, but I have yet


Why does it invoke undefined behaviour? Could you refer me to a
relevant section in the standard that states the reason? And where in
the standard does it state that & -> cancel each other out?
to see a real world implementation where it doesn't work as intended.

Dan

Nov 13 '05 #5
Mantorok Redgormor <ne*****@tokyo.com> scribbled the following:
Da*****@cern.ch (Dan Pop) wrote in message news:<bk**********@sunnews.cern.ch>...
In <41*************************@posting.google.com> ne*****@tokyo.com (Mantorok Redgormor) writes:
>#define FOO(x,y) ((int)&(((x*)NULL)->y))
>
>How is this legal? It is accessing the member of a struct whose
>address is "zero". Since NULL is zero.
It's not legal, but it works because it doesn't actually access the
member, it merely takes its address. If you replace int by size_t, you
get the typical implementation of the offsetof macro from <stddef.h>.

Strictly speaking, it is invoking undefined behaviour, but I have yet Why does it invoke undefined behaviour? Could you refer me to a
relevant section in the standard that states the reason? And where in
the standard does it state that & -> cancel each other out?


It invokes undefined behaviour *precisely* because the standard does
*not* say that & and -> cancel each other out.
to see a real world implementation where it doesn't work as intended.


This is irrelevant. As long as the standard doesn't require it, an
implementation where this sort of macro would not work is fully legal.
The compiler writer knows what works on his/her compiler and what does
not - therefore he/she can write macros like these like no one's
business, as long as they don't directly contradict the standard. You,
on the other hand (meaning Mantorok, Dan Pop, myself, and pretty much
everyone else), can not.

--
/-- 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! ------------/
"This isn't right. This isn't even wrong."
- Wolfgang Pauli
Nov 13 '05 #6

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

Similar topics

2
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
3
by: Alexander Ulyanov | last post by:
Hi all. Is it possible to pass the whole blocks of code (possibly including " and ,) as macro parameters? I want to do something like: MACRO(FOO, "Foo", "return "Foobar";", "foo();...
8
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
5
by: Bill | last post by:
This database has no forms. I am viewing an Access table in datasheet view. I'd like to execute a macro to execute a function (using "runcode"). In the function, I'll reading data from the record...
4
by: gareth | last post by:
Hello all, I've done C before but no C++. I'm looking at the example BHO at: http://www.adp-gmbh.ch/win/com/bho.html and I'm a bit confused by some of the code: 1. class BHO class adpbho :...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
11
by: !truth | last post by:
Hi, i feel confused about the following program, and it's works to get a pointer-member's address. #define NETDEV_ALIGN 32 #define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1) ...
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:
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.