g++ "offsetof" problem

Discussion in 'C++' started by Hiroki Horiuchi, Nov 25, 2003.

  1. 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
    {
    private:
    int x;
    int y;
    public:
    static unsigned int const y_offset;
    };

    unsigned int const A::y_offset = (
    reinterpret_cast<char *>(&static_cast<A *>(0)->y)
    -
    static_cast<char *>(0)
    );

    I want to define a constant like A::y_offset with g++ without warnings.
    Is it possible?

    Thank you in advance.

    //Hiroki Horiuchi
     
    Hiroki Horiuchi, Nov 25, 2003
    #1
    1. Advertising

  2. Hiroki Horiuchi

    Attila Feher Guest

    Hiroki Horiuchi wrote:
    > 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
    > {
    > private:
    > int x;
    > int y;
    > public:
    > static unsigned int const y_offset;
    > };
    >
    > unsigned int const A::y_offset = (
    > reinterpret_cast<char *>(&static_cast<A *>(0)->y)
    > -
    > static_cast<char *>(0)
    > );
    >
    > I want to define a constant like A::y_offset with g++ without
    > warnings.
    > Is it possible?


    Nope. offsetof is only valid for C structs. You will need to rethink your
    design.

    --
    Attila aka WW
     
    Attila Feher, Nov 25, 2003
    #2
    1. Advertising

  3. Hiroki Horiuchi wrote in
    news::

    > 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
    > {
    > private:
    > int x;
    > int y;
    > public:
    > static unsigned int const y_offset;
    > };
    >
    > unsigned int const A::y_offset = (
    > reinterpret_cast<char *>(&static_cast<A *>(0)->y)
    > -
    > static_cast<char *>(0)
    > );


    static_cast<A *>(0) is NULL pointer you may not dereference it any
    portable way. Note the Standard says that the inbult operator ->
    derefences the pointer that its applied to, so it does even though
    all you do is take an address.

    >
    > I want to define a constant like A::y_offset with g++ without
    > warnings. Is it possible?
    >


    Look into the offsetof macro and use it correctly,

    #include <iostream>
    #include <cstdlib>

    /* struct/class must be a POD (Plain Old Data) to be
    sutable for use with offsetof
    */
    struct A_POD
    {
    int x;
    int y;
    int z;
    };

    class A : private A_POD
    {
    public:

    int &operator [] ( unsigned off );

    static unsigned int const offset[ 3 ];

    A( int xx, int yy, int zz )
    {
    x = xx;
    y = yy;
    z = zz;
    }
    };

    unsigned int const A::eek:ffset[ 3 ] =
    {
    offsetof( A_POD, x ),
    offsetof( A_POD, y ),
    offsetof( A_POD, z )
    };


    int & A::eek:perator [] ( unsigned off )
    {
    return *reinterpret_cast< int * >(
    reinterpret_cast< char * >(
    static_cast< A_POD * >( this )
    )
    +
    offset[ off ]
    );
    }


    int main()
    {
    A a( 1, 2, 3 );

    std::cerr
    << a[ 0 ] << "\n"
    << a[ 1 ] << "\n"
    << a[ 2 ] << "\n"
    ;
    }

    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Nov 25, 2003
    #3
  4. Hiroki Horiuchi

    tom_usenet Guest

    On 25 Nov 2003 04:48:40 -0800, (Hiroki Horiuchi)
    wrote:

    >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
    >{
    >private:
    > int x;
    > int y;
    >public:
    > static unsigned int const y_offset;
    >};
    >
    >unsigned int const A::y_offset = (
    > reinterpret_cast<char *>(&static_cast<A *>(0)->y)
    >-
    > static_cast<char *>(0)
    >);
    >
    >I want to define a constant like A::y_offset with g++ without warnings.
    >Is it possible?


    Possibly, but it isn't portable anyway. offsetof can only be used with
    POD types (which can't have private data). This compiles without
    warnings:

    #include <stddef.h>

    class A
    {
    public:
    int x;
    int y;
    static unsigned int const y_offset;
    };

    unsigned int const A::y_offset = offsetof(A, y);

    Tom
     
    tom_usenet, Nov 25, 2003
    #4
  5. Hiroki Horiuchi

    Ron Natalie Guest

    "Hiroki Horiuchi" <> wrote in message news:...
    > 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)
    >

    It's undefiend to use offsetof on non-POD's.
     
    Ron Natalie, Nov 25, 2003
    #5
  6. Hiroki Horiuchi

    red floyd Guest

    Hiroki Horiuchi wrote:

    > 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
    > {
    > private:
    > int x;
    > int y;
    > public:
    > static unsigned int const y_offset;
    > };
    >
    > unsigned int const A::y_offset = (
    > reinterpret_cast<char *>(&static_cast<A *>(0)->y)
    > -
    > static_cast<char *>(0)
    > );
    >
    > I want to define a constant like A::y_offset with g++ without warnings.
    > Is it possible?
    >
    > Thank you in advance.
    >
    > //Hiroki Horiuchi


    You can't get the "offset" of it, but you could do something like this:

    class A
    {
    private:
    int x;
    int y;
    public:
    static int A::* y_offset;
    }

    int A::* A::y_offset = &A::y;
     
    red floyd, Nov 25, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Johansson

    offsetof

    Tony Johansson, Dec 16, 2004, in forum: C++
    Replies:
    1
    Views:
    461
    Alf P. Steinbach
    Dec 16, 2004
  2. Tony Johansson

    More offsetof

    Tony Johansson, Dec 16, 2004, in forum: C++
    Replies:
    3
    Views:
    520
    Mike Wahler
    Dec 18, 2004
  3. Exits Funnel

    Trouble with offsetof

    Exits Funnel, Jan 8, 2005, in forum: C++
    Replies:
    9
    Views:
    534
    Exits Funnel
    Jan 9, 2005
  4. Alejo

    offsetof

    Alejo, Jul 15, 2003, in forum: C Programming
    Replies:
    20
    Views:
    1,074
    Dan Pop
    Jul 22, 2003
  5. Arthur J. O'Dwyer

    Pre-offsetof() question

    Arthur J. O'Dwyer, Oct 6, 2003, in forum: C Programming
    Replies:
    6
    Views:
    414
    Arthur J. O'Dwyer
    Oct 7, 2003
Loading...

Share This Page