A doubt about struct

Discussion in 'C Programming' started by Alex, Apr 13, 2006.

  1. Alex

    Alex Guest

    If I have two struct. See below:
    struct s1
    {
    int type;
    int (*destroy)(struct s1* p);
    }

    struct s2
    {
    struct s1 base;
    .../* here are some other memebers */
    }.

    My doubt is that Will the standard C will guarantee the cast from
    'struct s2' to 'struct s1' is safe. The problem comes because I don't
    know
    whether the align in struct will affect such cast.
    Alex, Apr 13, 2006
    #1
    1. Advertising

  2. Alex

    Michael Mair Guest

    Alex schrieb:
    > If I have two struct. See below:
    > struct s1
    > {
    > int type;
    > int (*destroy)(struct s1* p);
    > }
    >
    > struct s2
    > {
    > struct s1 base;
    > .../* here are some other memebers */
    > }.
    >
    > My doubt is that Will the standard C will guarantee the cast from
    > 'struct s2' to 'struct s1' is safe. The problem comes because I don't
    > know whether the align in struct will affect such cast.


    Do not cast from struct s2 to struct s1.

    The standard guarantees to you that the address of the first
    member of a struct is the same as the address of the struct
    itself.

    This means you can cast from "struct s2 *" to a pointer to
    the type of the first member, "struct s1 *".

    There is no conversion defined between the structure types
    themselves. If you need the value, you should reinterpret
    the address, i.e. for
    struct s1 S1;
    struct s2 S2 = {.....};
    both are equivalent:
    S1 = S2.base;
    S1 = *((struct s1 *)&S2);
    whereas your compiler should complain about
    "S1 = (struct s1) S2;" which is equivalent to "S1 = S2" (the
    former an explicit, the latter an implicit conversion to
    struct s1).


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Apr 13, 2006
    #2
    1. Advertising

  3. Alex wrote:
    > If I have two struct. See below:
    > struct s1
    > {
    > int type;
    > int (*destroy)(struct s1* p);
    > }
    >
    > struct s2
    > {
    > struct s1 base;
    > .../* here are some other memebers */
    > }.
    >
    > My doubt is that Will the standard C will guarantee the cast from
    > 'struct s2' to 'struct s1' is safe. The problem comes because I don't
    > know
    > whether the align in struct will affect such cast.


    You can only cast scalar types, not composite types like structures, so
    I assume that you mean a cast from (struct s2 *) to (struct s1 *). The
    C99 standard (6.7.2.1) specifies that a pointer to a structure object,
    suitably converted, points to its initial member. Therefore, your
    proposed cast is legal and guaranteed to work.

    --
    Diomidis Spinellis
    Code Quality: The Open Source Perspective (Addison-Wesley 2006)
    http://www.spinellis.gr/codequality
    Diomidis Spinellis, Apr 13, 2006
    #3
  4. Alex

    Alex Guest

    Michael Mair wrote:
    > Alex schrieb:
    > > If I have two struct. See below:
    > > struct s1
    > > {
    > > int type;
    > > int (*destroy)(struct s1* p);
    > > }
    > >
    > > struct s2
    > > {
    > > struct s1 base;
    > > .../* here are some other memebers */
    > > }.
    > >
    > > My doubt is that Will the standard C will guarantee the cast from
    > > 'struct s2' to 'struct s1' is safe. The problem comes because I don't
    > > know whether the align in struct will affect such cast.

    >
    > Do not cast from struct s2 to struct s1.
    >
    > The standard guarantees to you that the address of the first
    > member of a struct is the same as the address of the struct
    > itself.
    >
    > This means you can cast from "struct s2 *" to a pointer to
    > the type of the first member, "struct s1 *".
    >
    > There is no conversion defined between the structure types
    > themselves. If you need the value, you should reinterpret
    > the address, i.e. for
    > struct s1 S1;
    > struct s2 S2 = {.....};
    > both are equivalent:
    > S1 = S2.base;
    > S1 = *((struct s1 *)&S2);
    > whereas your compiler should complain about
    > "S1 = (struct s1) S2;" which is equivalent to "S1 = S2" (the
    > former an explicit, the latter an implicit conversion to
    > struct s1).


    I cannot express my idea well, my intention is pointer cast.
    So, if I do like this:
    struct s1* p = malloc(sizeof(struct s2));
    And then, I use(read or write) some members of struct s1 via the
    pointer p, is it OK?
    Alex, Apr 13, 2006
    #4
  5. Alex

    Michael Mair Guest

    Alex schrieb:
    > Michael Mair wrote:
    >>Alex schrieb:
    >>
    >>>If I have two struct. See below:
    >>>struct s1
    >>>{
    >>> int type;
    >>> int (*destroy)(struct s1* p);
    >>>}
    >>>
    >>>struct s2
    >>>{
    >>> struct s1 base;
    >>> .../* here are some other memebers */
    >>>}.
    >>>
    >>>My doubt is that Will the standard C will guarantee the cast from
    >>>'struct s2' to 'struct s1' is safe. The problem comes because I don't
    >>>know whether the align in struct will affect such cast.

    >>
    >>Do not cast from struct s2 to struct s1.
    >>
    >>The standard guarantees to you that the address of the first
    >>member of a struct is the same as the address of the struct
    >>itself.
    >>
    >>This means you can cast from "struct s2 *" to a pointer to
    >>the type of the first member, "struct s1 *".
    >>
    >>There is no conversion defined between the structure types
    >>themselves. If you need the value, you should reinterpret
    >>the address, i.e. for
    >> struct s1 S1;
    >> struct s2 S2 = {.....};
    >>both are equivalent:
    >> S1 = S2.base;
    >> S1 = *((struct s1 *)&S2);
    >>whereas your compiler should complain about
    >>"S1 = (struct s1) S2;" which is equivalent to "S1 = S2" (the
    >>former an explicit, the latter an implicit conversion to
    >>struct s1).

    >
    > I cannot express my idea well, my intention is pointer cast.
    > So, if I do like this:
    > struct s1* p = malloc(sizeof(struct s2));
    > And then, I use(read or write) some members of struct s1 via the
    > pointer p, is it OK?


    Yes, this is okay. I am not entirely sure what you want to
    achieve, though. The above is a little bit too short.
    Please give us a little bit more context, e.g. whether you
    have a function allocating new storage for struct s2 and you
    want to use this just as struct s1 or even a little bit more.
    Maybe there is an even better way of doing what you want to
    do.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Apr 13, 2006
    #5
  6. Alex

    Alex Guest

    Michael Mair wrote:
    > Yes, this is okay. I am not entirely sure what you want to
    > achieve, though. The above is a little bit too short.
    > Please give us a little bit more context, e.g. whether you
    > have a function allocating new storage for struct s2 and you
    > want to use this just as struct s1 or even a little bit more.
    > Maybe there is an even better way of doing what you want to
    > do.


    What I want to do is something like Interface in Java or abstract class
    in C++.
    In the struct s1, I will declare some function pointers, which I will
    not
    assign for them. And in struct s2, I will delcare a object of struct
    s1,
    then assign for the function pointers of the object. The intention of
    doing
    all of above is to implement simple OOP using C.( Because In my
    enviroment,
    I cannot use other language.)
    So, if you can provide any information , even a link, about how to
    implement simple
    OOP, I will appreciate your help very much.
    Alex, Apr 13, 2006
    #6
  7. On Thu, 13 Apr 2006 10:52:16 +0200,
    Michael Mair <> wrote
    in Msg. <>

    > Yes, this is okay. I am not entirely sure what you want to
    > achieve, though.


    Probably some OO-like stuff in C. The GTK+ toolkit used to be written
    like that, and now probably GObject is.

    robert
    Robert Latest, Apr 13, 2006
    #7
  8. Alex

    Michael Mair Guest

    Alex schrieb:
    > Michael Mair wrote:
    >
    >>Yes, this is okay. I am not entirely sure what you want to
    >>achieve, though. The above is a little bit too short.
    >>Please give us a little bit more context, e.g. whether you
    >>have a function allocating new storage for struct s2 and you
    >>want to use this just as struct s1 or even a little bit more.
    >>Maybe there is an even better way of doing what you want to
    >>do.

    >
    > What I want to do is something like Interface in Java or
    > abstract class in C++.
    > In the struct s1, I will declare some function pointers, which
    > I will not assign for them. And in struct s2, I will delcare a
    > object of struct s1, then assign for the function pointers of
    > the object. The intention of doing all of above is to implement
    > simple OOP using C.( Because In my enviroment, I cannot use
    > other language.)
    > So, if you can provide any information , even a link, about how
    > to implement simple OOP, I will appreciate your help very much.


    There have been several discussions about this in comp.lang.c;
    maybe you can find something useful searching the archives:

    <http://groups.google.de/groups?as_q=OOP+in+C&num=30&scoring=r&hl=de&as_epq=&as_oq=&as_eq=&as_ugroup=comp.lang.c&as_usubject=&as_uauthors=&lr=&as_qdr=&as_drrb=b&as_mind=1&as_minm=1&as_miny=2004&as_maxd=17&as_maxm=4&as_maxy=2006>

    The basic technique of "typesafe casting", always remains the
    same: You have a pointer to a structure; you know that the
    first member of this structure always is or contains (maybe
    over several levels) a structure describing a "parent class".
    You always can cast a pointer of a "child class" to a pointer
    of the "parent class". This of course restricts you to single
    inheritance.
    Often, all "classes" are derived from one generic "class" which
    may even offer a way to find out the "type" of the "class" it
    is a member of. In this case, it is safe to cast "from small to
    large", i.e. from the pointer of the "parent class" to a pointer
    to the "child class"; this can become the source of merry
    confusion if not done right.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Apr 17, 2006
    #8
    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. Chris Fogelklou
    Replies:
    36
    Views:
    1,355
    Chris Fogelklou
    Apr 20, 2004
  2. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    600
  3. Replies:
    0
    Views:
    541
  4. Peter Otten
    Replies:
    2
    Views:
    103
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    109
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page