| |||||
Technical Support Support Resources
Product Information | C251: POINTER ARITHMETIC DELIVERS UNEXPECTED RESULTSInformation in this article applies to:
QUESTIONI have found a serious problem when I use pointer arithemetic with a generic memory allocation function. The results generated seem to consider only the 16-bit offset rather than the full address space of the 251 architecture (which is up to 16MB). Example: typedef unsigned char BYTE; typedef unsigned long DWORD; BYTE *MemStart; // first available memory location BYTE *GetLastAddress (void); // returns last available memory location DWORD freeSpace; // this generates a incorrect result: freeSpace=(DWORD) GetLastAddress() - MemStart); // generates incorrect result ANSWERA pointer subtraction delivers the number of elements within an array that is addressed by these pointers. By default the C251 uses far pointers. This implies that the pointers point to the same elements within a single 64KB segment. To get the total memory that is between unrelated pointers (that point to any memory location) use a explicit cast to huge * as shown below: freeSpace=(DWORD)((BYTE huge *)GetLastAddress() - (BYTE huge *)MemStart); Of course an cast to DWORD will work too and is maybe more transparent to understand: freeSpace= (DWORD)GetLastAddress() - (DWORD)MemStart; MORE INFORMATION
SEE ALSO
Last Reviewed: Tuesday, December 18, 2007 | ||||
| |||||