Thread: (;_;) please don't think constant `offsetof' is portable

  1. #1
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108

    (;_;) please don't think constant `offsetof' is portable

    This applies to C and C++.

    Please don't assume that just because the standard says some things about a facility that the real world will fit exactly.

    The `offsetof' facility is neat, but please don't use it everywhere as if it is a constant expression.

    I was just asked to help "port" some code to a new compiler that uses `offsetof' to calculate static buffer sizes.

    Most implementations of `offsetof' depend on pointer arithmetic which can't be used in a constant expression such as the side of a static buffer.

    It is not just "home grown" implementations. The statement holds for many implementations that ship with compilers.

    So again, please don't try to treat `offsetof' as a constant.

    Soma

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Better yet, avoid using offsetof if at all possible.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 08-22-2011, 08:39 AM
  2. c offsetof macro. quick question
    By beheadedmyway in forum C Programming
    Replies: 2
    Last Post: 10-01-2010, 09:45 PM
  3. offsetof
    By hzmonte in forum C Programming
    Replies: 10
    Last Post: 10-27-2008, 08:57 AM
  4. Constant pointer to constant value
    By tretton in forum C Programming
    Replies: 10
    Last Post: 12-23-2005, 01:45 PM
  5. offsetof
    By Dave_Sinkula in forum C Programming
    Replies: 2
    Last Post: 06-21-2003, 02:04 PM