Offset of a member in a structure - General Discussion and Assistance - CHDK Forum
supplierdeeply

Offset of a member in a structure

  • 1 Replies
  • 1253 Views
Offset of a member in a structure
« on: 06 / November / 2010, 09:01:34 »
Advertisements
I am having a very strange problem computing the offset of a member in a structure.

Consider this  ....

In gui.c at start :-

int gConfOffset;
char *gpBaseAddress = &conf.save_raw.
char *gpMemberAddress = NULL;

In gui_dist_mode_enum() (not in CHDK) :-

gpMemberAddress=(char *)&conf.dist_mode;
gConfOffset=gpMemberAddress-gpBaseAddress;

If you print the member and base address, the calculated offset value is correct.

If similar code is put in gui_tv_enum() and :-

gpMemberAddress = &conf.tv_override

the calculated offset is incorrect !

I find that impossible to explain.

You can also use the macro :-

#define GetOffset( struct_type, member ) ((int)&((struct_type*)0)->member)

but it makes no difference.

I will try inserting the code into other enumerations.

Any possible explanation for this ?


*

Offline reyalp

  • ******
  • 12106
Re: Offset of a member in a structure
« Reply #1 on: 06 / November / 2010, 16:31:22 »
I suggest posting actual code, not "similar code in... fails"

What are the correct and incorrect values ? These may give you some clue as to what is going wrong.

What are the types of the various things involved ?

If you can narrow it down to a minimal test case (a simple struct two members) that will be much easier than having it mixed into irrelevant code we can't see.

Other things to try:
Look at the generated machine code see what it is doing (core/main.dump if using something like chdk build process)

If you come up with a simple test case, try it on PC.
Don't forget what the H stands for.

 

Related Topics