Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

indexing out from a struct member

Name: Anonymous 2011-11-14 3:29

Has anyone here ever used this trick before?


#define OFFSET_IN_STRUCT(struct_name, member) \
  &(((struct_name*)0)->member)

#define ADDRESS_OF_PARENT(struct_name, member, member_address) \
  ((member_address) - OFFSET_IN_STRUCT(struct_name, member))


And then say you have data types, A, B, and C, that each form some kind of linked structure with other instances of the same data type. But in this scenario, every instance is actually a member of a struct D, and you can get a pointer to the D instance from a pointer to one of its members.


struct D {
  struct A thingy;
  struct B other_thingy;
  struct C extra_thingy;
};

struct C* C_get_next_thingy(struct C*)

struct D* D_get_next_extra_thingy(struct D* self) {
  return ADDRESS_OF_PARENT(
           struct D,
           extra_thingy,
           C_get_next_thingy(&(self->extra_thingy)));
}


So now the C data type can be implemented once, and used in many other data types, to provide the same linked structure for other data types. I assume the only other way to get this would be with inheritance, where D would have to inherit from C. But then D would also have to inherit from A and B, if one was to try to do this with them as well. In a language where multiple inheritance was not allowed, I guess you would just have to make a derived class for each of the A, B, C data types, and give each a reference to the master D instance that owned them. Then the conversion could be:


class D {
  private A thingy;
  private B other_thingy;
  private C extra_thingy;

  public D get_next_extra_thingy() {
    return extra_thingy.get_next_thingy().get_D_reference();
  }
}


I'm trying to decide if this approach would be better than just using multiple inheritance in general. It seems more organized to me, but it might end up being more overhead. Thoughts?

Name: FrozenVoid !!mJCwdV5J0Xy2A21 2011-11-14 3:39

Struct have very low overhead. Classes and virtual function tables are much more wasteful.
You either have ncie abstractions that costs some performance(in most cases near zero) or you use some hack for approximating the abstraction(and sometimes have more flexible,lower-level abstraction). In your case its reinvention of:
http://en.wikipedia.org/wiki/Offsetof

Name: Anonymous 2011-11-14 10:59

>>2

thanks for the link. I didn't know this was already provided in ansi C.

Name: Anonymous 2011-11-14 15:33

>>2
whenever I read about things like this I can't help but be astonished by the cleverness of some programmers. Still, I'm drawn more the clean and tight feel that Haskell gives me. Meanwhile, I have never really progressed much since SICP so I mostly program in Racket, planning on reading the Haskell typeclassopedia soon though...

Name: Anonymous 2011-11-14 15:48

Hum didn't know there was a function for this but used this method before yes.

Name: Anonymous 2011-11-14 18:53

>>5
It's a macro, not a function.

Name: Anonymous 2011-11-14 21:04

You gave me a big boner, OP.
I really have to leave (fucking ENTERPRISE) Java for C.

Don't change these.
Name: Email:
Entire Thread Thread List