| 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 <windows.h>
#include <stddef.h>
#include <stdio.h>
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 <stddef.h> file is included, because the offsetof() macro is defined in this file.
Source
Preparing Code for the IA-64 Architecture (Code Clean)
For more complete information about compiler optimizations, see our Optimization Notice.

