This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Offset to a member of a structure (relative to that structure)

I have a structure I would like to compute the offset to members within.

I've attempted this numerous ways all of which fail to generate a CONSTANT offset instead the compiler insists and create code for what is essentially the subtraction of 2 constants. (mutter) so

typedef unsigned short uint16_t;
#define SENSOR_TYPE_OFFSET              ((uint16_t)((char *)&ralph.constants.some_stuff.silly_putty) - (uint16_t)((char *)&ralph))


generates lots of cute code but not a constant offset to the member of the type ralph is. So are there any suggestions of beating the compiler into doing what I want?
I would rather not compute the offsets by hand. I suppose I could add up the sizeof() for each member also but either one is kind of annoyingly messy and very ugly.

  • Actually, ANSI C has a macro for that. It's called offsetof(), and it's defined in stddef.h. Hopefully, Keil implemented it to generate efficient code.

  • Adding up individual sizeof() is very, very, very bad!

    Does Keil see the full declaration of your pointer? What type of pointer is it? A generic pointer, or XDATA etc?

  • just a couple of complementing comments:
    Per's assertion that
    Adding up individual sizeof() is very, very, very bad!
    are based on portability issues: the compiler might insert padding into your structure to make it more comfortable to work with. if so, adding up elements size might leave your program very dead. always use something like 'offsetof', which is a macro that looks like this (for Keil):

    #define offsetof(t, memb) ((__CLIBNS size_t)__INTADDR__(&(((t *)0)->memb)))
    

    notice how 0 is cast to a pointer to the proper type and member.

  • "...I would like to compute the offset to members..."

    Why do you want to do that?

    Perhaps if you explained what you're actually trying to achieve here, people might be able to suggest ways to achieve that - rather than just discussing the demerits of what may be a fundamentally flawed approach...?

  • Well it is rather simple (surprisingly) and the reason I asked was I could not find offsetof() macro (sad). Erstwhile I found the correct header to include and I have to admit I looked at the macro, much simpler than what I had thought of (is this a surprise?)

    As for the "Why?" I've moved data structures that were stored in XDATA space (parallel EEPROM) into a serial EEPROM, this means of course getting access to specific bits of data within this structure is no longer as easy as

    ralph.constants.some_stuff.silly_putty
    

    so I needed

    offsetof(ralph, constants.somestuff.sillyputty)
    

    in addition to the address of ralph (ralph is easily computable in a portable manner, the offset needs to be computed in a portable manner by code requirements). The structures are too large to read in just to access 1 byte (limited memory space and time) therefore the obvious solution was to just get the byte of data desired. The structures are stored as a large (IE > 15) sequential array therefore static address computation is .. useless, however static offset computation isn't.

    Not very exciting I'm afraid.

    Stephen