Search code examples
c++pointersnew-operator

In C++ 64-bit environment, is there a case where the result Pointer of the new operator uses all 8 bytes?


I use Windows 10 OS Pc and build in environment debug x64 mode. I used Visual Studio 2019.

example code

int * d = new int;

-Memory-

&d is  30 45 0f 2f 12 02 00 00

Actually, in this case, it seems that the upper 2 bytes are not used.

Is there a case to use up to this part?.

I'm trying to put a memory usage count in this empty space(2byte).


Solution

  • In C++ 64-bit environment, is there a case where the result Pointer of the new operator uses all 8 bytes?

    There is no general guarantee applying to all 64 bit CPU architectures, that some of the bytes of a pointer would be unused.

    In latest implementations of the x86-64 architecture, the high 16 bits of a virtual address are required to be copies of the highest preceding bit, leaving only 48 bits for the address space. The specification allows this to be extended up to the full 64 bits in future (256 TB ought to be enough for anybody though... right?).

    The ability to take advantage of the "unused" bytes of the CPU architercture depends on whether the language implementation (operating system, compiler, sanitisers, ...) uses them already.

    I'm trying to put a memory usage count in this empty space(2byte).

    Using the high bytes of a pointer may be possible as long as you are aware that it will restrict the portability of your program to other operating systems and CPUs. Consider if you care whether your program still works in 10 years.

    You must however be careful to restore the high bits before indirecting through the pointer on x86-64 even though they are "unused" (see the reason above).