Menu

#693 gcc 3.4 and FIELD_OFFSET

WSL
closed
nobody
None
works-for-me
Unknown
2013-01-31
2004-12-10
No

I have been using some code that makes use of the
following define from winnt.h
#define FIELD_OFFSET(t,f) ((LONG)&(((t*)0)->f))

Unfortunately this doesn't seem to be a valid constant
expression in the new c++ parser in 3.4? And I get
errors when I try and make use of it in case
statements. Could we make use of the offsetof macro in
stddef.h to fix this?

Discussion

  • Danny Smith

    Danny Smith - 2004-12-10

    Logged In: YES
    user_id=11494

    What stops you from:
    #ifdef __GNUC__
    #include <stddef.h>
    #undef FIELD_OFFSET
    #define FIELD_OFFSET(t,f) offsetof(t,f)
    #endif

    Danny

     
  • Dimitri Papadopoulos

    Logged In: YES
    user_id=52414

    But shouldn't this be fixed directly in w32api by replacing:
    #define FIELD_OFFSET(t,f) ((LONG)&(((t*)0)->f))
    by the offsetof macro or better a copy of the offsetof macro
    to avoid creating a dependency on <stddef.h>:

    #ifndef __cplusplus
    #define FIELD_OFFSET(t,f) ((LONG)&(((t*)0)->f))
    #else
    #define FIELD_OFFSET(t,f) (reinterpret_cast<LONG> \ (&reinterpret_cast<char&>(static_cast<t*>(0)->f)))
    #endif

     
  • Dimitri Papadopoulos

    • status: open --> closed-fixed
     
  • Dimitri Papadopoulos

    Logged In: YES
    user_id=52414

    The problem you report has caused modification in some fashion in the official CVS for the given package. The w32api and
    mingw-runtime official CVS reside in the winsup CVS directory tree for Cygwin. Those package CVS trees are periodically
    merged into the MinGW CVS tree. If you still find problems then please open a new report.

     
  • Danny Smith

    Danny Smith - 2006-04-20

    Logged In: YES
    user_id=11494

    The fix is broken:
    Here is a testcase.

    #include <stddef.h>

    #define FIELD_OFFSET(t,f) (reinterpret_cast<long> \ (&reinterpret_cast<char&>(static_cast<t*>(0)->f)

    struct choke_me
    {
    int size;
    char storage[1];
    };

    struct is_offset_broken
    {
    #ifdef USE_STDDEF
    static const int offset = offsetof (choke_me, storage);
    #else
    static const int offset = FIELD_OFFSET (choke_me,
    storage);
    #endif
    };

    The safe way to change the meaning of FIELD_OFFSET would
    be to include stddef.h. Note the gcc internal offsetof
    API for C++ has changed between gcc-2.95->gcc-3.4.x-
    >gcc.4.x. Note also that Watcom uses w32api so needs a
    __GNUC__ guard.

    Things like CONTAINING_RECORD would also need change for
    consistency???

    But why should we change the meaning of the w32api define
    FIELD_OFFSET anyway? If FIELD_OFFSET, as defined by MS,
    is not a valid constant expression in C++, the user should
    be told so.

    Danny

     
  • Danny Smith

    Danny Smith - 2006-04-20
    • status: closed-fixed --> open
     
  • Dimitri Papadopoulos

    Logged In: YES
    user_id=52414

    About this part of the comment:
    But why should we change the meaning of the w32api
    define FIELD_OFFSET anyway? If FIELD_OFFSET, as
    defined by MS, is not a valid constant expression
    in C++, the user should be told so.

    It's just that the GNU compiler doesn't like Microsoft's
    FIELD_OFFSET macro. The Visual C++ compiler does like it.

    Besides, the Microsoft implementation of FIELD_OFFSET is
    irrelevant. As far as I can see the macro is not documented
    in MSDN, at least I can't find anything in the current
    online version.

    Unlike Microsoft, we need to provide a FIELD_OFFSET macro
    that works with compilers other than Visual C++.

     
  • Dimitri Papadopoulos

    Logged In: YES
    user_id=52414

    Which compiler version fails to compile?

    If the offsetof has indeed been changed between gcc 2.95.*,
    gcc 3.*, and gcc 4.*, then we would need different versions
    of FIELD_OFFSET for different GNU compiler versions.

    Additionally the Watcom compiler is indeed far from being
    standard-compliant, I suspect it won't like the new
    FIELD_OFFSET either.

    So indeed... It looks like a nightmare to maintain a
    FIELD_OFFSET macro identical to the offsetof macro bundled
    with the compiler itself. It's probably better to have the
    dependency on <stddef.h>...

    Does the Watcom compiler define offsetof in <stddef.h>?

    Do I add the <stddef.h> trick for the GNU compiler only? In
    C++ mode only?

    About CONTAINING_RECORD, I wasn't aware of this macro. Will
    have to look into this.

     
  • Dimitri Papadopoulos

    Logged In: YES
    user_id=52414

    The problem you report has caused modification in some fashion in the official CVS for the given package. The w32api and
    mingw-runtime official CVS reside in the winsup CVS directory tree for Cygwin. Those package CVS trees are periodically
    merged into the MinGW CVS tree. If you still find problems then please open a new report.

     
  • Dimitri Papadopoulos

    • status: open --> closed-works-for-me
     
  • Dimitri Papadopoulos

    Logged In: YES
    user_id=52414

    Now I'm completely lost.

    I have reinstalled MinGW 3.1.0 and the following updates:
    gcc-core-3.4.5-20060117-1
    gcc-g++-3.4.5-20060117-1
    w32api-3.7
    mingw-runtime-3.8

    My test case now always compiles. Both the original
    FIELD_OFFSET and the new reinterpet_cast-based macro work
    for me.

    Here is what I'm going to do. I'm going to revert to the
    previous version (single FIELD_OFFSET definition and no C++
    special case) and close the case.

    If you still see problems, please reopen and specify which
    compiler the original FIELD_OFFSET fails with, and modify
    the test case to expose the problem.

     
  • Dimitri Papadopoulos

    test case

     
  • Earnie Boyd

    Earnie Boyd - 2013-01-31
    • labels: w32api (deprecated use WSL) -->
    • status: closed-works-for-me --> closed
    • resolution: --> works-for-me
    • category: --> Unknown
    • milestone: --> WSL
     
  • Earnie Boyd

    Earnie Boyd - 2013-01-31

    I've stated this as an Unknown because the OP never responded to questions.