Struct introspection or information

  • Good day everyone - sorry if this is not totally Cocoa-related: if
    there's a better list for this, please let me know and accept my
    apologies in advance...

    Is there any way to know the fields in a C structure, if you don't
    have the headers that define it? Or at the very least, is it possible
    to access it's field values?

    Thanks!

    Jean Le Clerc.
  • No, not for a plain C struct.
    If it is actually an Objective-C object, then there is support for this.

    Monitus wrote:
    > Good day everyone - sorry if this is not totally Cocoa-related: if
    > there's a better list for this, please let me know and accept my
    > apologies in advance...
    >
    > Is there any way to know the fields in a C structure, if you don't
    > have the headers that define it? Or at the very least, is it possible
    > to access it's field values?
    >
    > Thanks!
    >
    > Jean Le Clerc.
  • On Nov 5, 2007, at 12:33 PM, Monitus wrote:

    > Good day everyone - sorry if this is not totally Cocoa-related: if
    > there's a better list for this, please let me know and accept my
    > apologies in advance...
    >
    > Is there any way to know the fields in a C structure, if you don't
    > have the headers that define it?

    No. The compiler uses a struct declaration to determine the type,
    size and offsets of each of its fields, as well as for determining
    the overall size of the struct itself. Once the C source is converted
    into assembly language instructions the field information is no
    longer needed.

    > Or at the very least, is it possible to access it's field values?

    If you're thinking it terms of a source debugger, no, since no
    symbolic information remains. You can certainly dump memory starting
    at the base address of the structure and try to figure out where the
    various fields are and what they might contain.

    steve
  • You might get some mileage out of the @encode() directive.  It
    produces a c-string encoding of a c type, which (with significant
    effort) can be parsed in order to traverse a c structure of that
    type.  These encodings do not contain the field names of structures,
    but they often contain structure tag names...

    More information is available in the section "Type Encodings" of
    The Objective-C 2.0 Programming Language

    dave

    On 5-Nov-07, at 1:33 PM, Monitus wrote:

    > Good day everyone - sorry if this is not totally Cocoa-related: if
    > there's a better list for this, please let me know and accept my
    > apologies in advance...
    >
    > Is there any way to know the fields in a C structure, if you don't
    > have the headers that define it? Or at the very least, is it
    > possible to access it's field values?
    >
    > Thanks!
    >
    > Jean Le Clerc.
  • I may have misunderstood, but I thought the OP mentioned not having
    access to the headers that define certain C structs. If that's the
    case, we're talking about opaque structures from the point of view of
    the caller, right?

    On Nov 5, 2007, at 4:34 PM, David Spooner wrote:

    >
    > You might get some mileage out of the @encode() directive.  It
    > produces a c-string encoding of a c type, which (with significant
    > effort) can be parsed in order to traverse a c structure of that
    > type.  These encodings do not contain the field names of
    > structures, but they often contain structure tag names...
    >
    > More information is available in the section "Type Encodings" of
    > The Objective-C 2.0 Programming Language
    >
    > dave
    >
    > On 5-Nov-07, at 1:33 PM, Monitus wrote:
    >
    >> Good day everyone - sorry if this is not totally Cocoa-related: if
    >> there's a better list for this, please let me know and accept my
    >> apologies in advance...
    >>
    >> Is there any way to know the fields in a C structure, if you don't
    >> have the headers that define it? Or at the very least, is it
    >> possible to access it's field values?
    >>
    >> Thanks!
    >>
    >> Jean Le Clerc.
  • The main issue is that a structure declaration is last used by the
    compiler to determine how to allocate memory for a structure
    instance, and what the offset and size of a field is so it can be
    accessed correctly. Once the assembly instructions corresponding to
    the original C statements has been generated there's no further need
    to know what the fields were named or what they actually refer to.

    On Nov 6, 2007, at 8:25 AM, Monitus wrote:

    > Thank you to all who replied... Yes, the original problem stated
    > that I didn't have access to any kind of structure definition...
    > Still, I tried the @encode suggestion, and it looks like it gave me
    > an incomplete result - like @encode(struct __MyStruct) gave
    > "{__My"... Strange... Anyway, I'm now looking for other ways of
    > doing what I need to do, a I seriously doubt i can get to the
    > structure info...
    >
    > Thanks again!
    >
    > Jean.
    >
    > On Nov 5, 2007, at 8:53 PM, Steve Christensen wrote:
    >
    >> I may have misunderstood, but I thought the OP mentioned not
    >> having access to the headers that define certain C structs. If
    >> that's the case, we're talking about opaque structures from the
    >> point of view of the caller, right?
    >>
    >>
    >> On Nov 5, 2007, at 4:34 PM, David Spooner wrote:
    >>
    >>>
    >>> You might get some mileage out of the @encode() directive.  It
    >>> produces a c-string encoding of a c type, which (with significant
    >>> effort) can be parsed in order to traverse a c structure of that
    >>> type.  These encodings do not contain the field names of
    >>> structures, but they often contain structure tag names...
    >>>
    >>> More information is available in the section "Type Encodings" of
    >>> The Objective-C 2.0 Programming Language
    >>>
    >>> dave
    >>>
    >>> On 5-Nov-07, at 1:33 PM, Monitus wrote:
    >>>
    >>>> Good day everyone - sorry if this is not totally Cocoa-related:
    >>>> if there's a better list for this, please let me know and accept
    >>>> my apologies in advance...
    >>>>
    >>>> Is there any way to know the fields in a C structure, if you
    >>>> don't have the headers that define it? Or at the very least, is
    >>>> it possible to access it's field values?
    >>>>
    >>>> Thanks!
    >>>>
    >>>> Jean Le Clerc.
  • Well, there's nothing preventing you from disassembling code that
    references the structure fields to determine the offsets and sizes...

    On Nov 6, 2007, at 12:04 PM, Monitus wrote:

    > Right - I thought that's what I understood too... I was just
    > wondering if maybe there was a way to somehow access the values in
    > a struct "instance" by offset or something... But I guess if I
    > can't know the size of each field, offsets are not usable either...
    >
    > Thanks!
    >
    > Jean.
    >
    > On Nov 6, 2007, at 1:28 PM, Steve Christensen wrote:
    >
    >> The main issue is that a structure declaration is last used by the
    >> compiler to determine how to allocate memory for a structure
    >> instance, and what the offset and size of a field is so it can be
    >> accessed correctly. Once the assembly instructions corresponding
    >> to the original C statements has been generated there's no further
    >> need to know what the fields were named or what they actually
    >> refer to.
    >>
    >>
    >> On Nov 6, 2007, at 8:25 AM, Monitus wrote:
    >>
    >>> Thank you to all who replied... Yes, the original problem stated
    >>> that I didn't have access to any kind of structure definition...
    >>> Still, I tried the @encode suggestion, and it looks like it gave
    >>> me an incomplete result - like @encode(struct __MyStruct) gave
    >>> "{__My"... Strange... Anyway, I'm now looking for other ways of
    >>> doing what I need to do, a I seriously doubt i can get to the
    >>> structure info...
    >>>
    >>> Thanks again!
    >>>
    >>> Jean.
    >>>
    >>> On Nov 5, 2007, at 8:53 PM, Steve Christensen wrote:
    >>>
    >>>> I may have misunderstood, but I thought the OP mentioned not
    >>>> having access to the headers that define certain C structs. If
    >>>> that's the case, we're talking about opaque structures from the
    >>>> point of view of the caller, right?
    >>>>
    >>>>
    >>>> On Nov 5, 2007, at 4:34 PM, David Spooner wrote:
    >>>>
    >>>>>
    >>>>> You might get some mileage out of the @encode() directive.  It
    >>>>> produces a c-string encoding of a c type, which (with
    >>>>> significant effort) can be parsed in order to traverse a c
    >>>>> structure of that type.  These encodings do not contain the
    >>>>> field names of structures, but they often contain structure tag
    >>>>> names...
    >>>>>
    >>>>> More information is available in the section "Type Encodings" of
    >>>>> The Objective-C 2.0 Programming Language
    >>>>>
    >>>>> dave
    >>>>>
    >>>>> On 5-Nov-07, at 1:33 PM, Monitus wrote:
    >>>>>
    >>>>>> Good day everyone - sorry if this is not totally Cocoa-
    >>>>>> related: if there's a better list for this, please let me know
    >>>>>> and accept my apologies in advance...
    >>>>>>
    >>>>>> Is there any way to know the fields in a C structure, if you
    >>>>>> don't have the headers that define it? Or at the very least,
    >>>>>> is it possible to access it's field values?
    >>>>>>
    >>>>>> Thanks!
    >>>>>>
    >>>>>> Jean Le Clerc.
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today