Ensure Accurate Data Access When Using An Offset with Itanium Architecture

Submit New Article

March 31, 2009 9:00 PM PDT


Challenge

Avoid misidentifying data fields due to hard-coded offsets being used to access data structures with Itanium® architecture. The following code contains a hard-coded offset to access a data item; when compiled for the Itanium architecture, the hard-coded offset might not correctly identify the field: MIGRATION:GENERIC-HEAD]-->

Solution

Change the hard-coded offset to an offset that is automatically determined according to the architecture (32-bit or 64-bit), which will allow the field to be correctly identified in either architecture. A variation on the code shown in the "Challenge" section above that gives an example of this technique is shown below:

#include &#60windows.h&#62
#include &#60stddef.h&#62
#include &#60stdio.h&#62

typedef struct _FOO {
PCHAR pszName;
DWORD dwVal;
} FOO, *PFOO;

int main(void) {
PFOO pFoo = new FOO;
...
DWORD dwVal= *(PDWORD)
((PBYTE)pFoo + offsetof(FOO, dwVal));
printf("dwVal=%d ", dwVal);
delete pFoo;
return 0;
}

The &#60stddef.h&#62 file is included, because the offsetof() macro is defined in this file.

Source

Preparing Code for the IA-64 Architecture (Code Clean)