1

Can a multi-byte primitive data type (ie int, double, float, etc) span multiple virtual pages on Windows?

ex) The first 4-bytes of a double on a virtual page, and the next 4-bytes of a double on the next virtual page. Is this possible*?

What about structs? Arrays?


*I presume it is technically possible to just cast the address of the last 4-bytes of arbitrary memory in a virtual page to a double pointer, as long as there is another virtual page that follows -- but I'm more curious if this can happen naturally in a compiled/interpreted program.

1

I don't see why not.

For instance, if I'm mapping a binary file into memory, I may configure its structure as such:

//Visual Studio code

#pragma pack(push,1)
struct STRUCT1{
    char dummy[0x1000 - 4];
    double fDouble;
};
#pragma pack(pop)

struct STRUCT2{
    STRUCT1 __declspec(align(0x1000)) s1;
};


int main()
{
    STRUCT2 s2 = {0};
    s2.s1.fDouble = -123.124;

    //This construct is just to give you the direct pointer
    double* pD = (double*)((BYTE*)&s2 + offsetof(STRUCT2, s1.fDouble));
    double fDVal = *pD;

}

Then if we break with a debugger on the last line and check the value of pD (that is in the RAX register in this screenshot):

enter image description here

enter image description here

You will see that the double variable is straddling the page boundary (marked in red):

enter image description here

This is far from ideal from the efficiency standpoint (due to those SSE2 instructions not "liking" being unaligned) but it is quite possible.

1

While it can happen as a result of hand-coded code, it is unlikely in practice to occur as a result of compiled code. Compilers generally align data appropriately such that a 4-bytes value will begin on a 4-bytes-aligned address, 8-bytes at 8-bytes, etc, to avoid alignment exceptions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.