Attempting to allocate C ++ dynamic memory

Im trying to allocate 1 KiB of memory using pointers

GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
        std::cout << pmc.WorkingSetSize << " Current physical memory used by the process" << std::endl;
        int a = pmc.WorkingSetSize;
        char *test= new char[1024];

GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
        int b = pmc.WorkingSetSize;
            std::cout << "Actual allocated  " << (b - a) / 1024 << std::endl;

The problem is that every time I run this code, it seems to allocate somewhere between 100 KiB to 400 KiB, I used char, since it is 1 byte

+4
source share
3 answers

, , () ( ). , - , , , .

, ++; , , , , , , -, .

+8

new . , . , , . , new .

, new 100 , 1 99 , , .

+4

::operator new ( ) ++ (- ++, , โ€‹โ€‹ n3337). ( , ++, OS - , ). , new delete ( , ).

: ( ).

, Microsoft . , Microsoft Windows. , . , , . , .


++, , (, - Linux mmap (2), , Windows, ). , process .

" , "

( ), (, VirtualAlloc, new malloc - LoadLibrary ). BTW, x86-64 ( , MMU) 4kbytes, , 1024 .

(. PS , Iinspectable Microsoft, " " , - : wikipedia, )

(, new, , ). new malloc ( C), , free -d , . ++ . .

, , , . , , , delete -d ( free -d) .

BTW, Linux, ++, ++, C โ€‹โ€‹free , , gory.

, ( mmap (2) Linux, VirtualAlloc , LoadLibrary , , Windows). BTW, Linux proc (5) ( /proc/self/maps), . , - Windows . VAS , " , , ", , (, VirtualAlloc Windows mmap Linux).

Iinspectable , , . wikipedia, 1970- IBM

, - 100 KiB 400 KiB

, ++ ( ), . , ASLR ( , ).

, ::operator new ( , , delete). , , :

void* ::operator new  ( std::size_t count ) { throw std::bad_alloc; }

. . malloc. !

( - , , , )


PS. , Microsoft ( Iinspectable ) " " , . wikipedia , Operating : (. 13). , ISA, ( IIRC 48 Ryzen, . figure x86-64 wikipage ). "segment " [] " , ", , . ( ). AMD (256 ) ยง2.1.5.1 AMD Ryzen, ( ). ( , MMU). , "" - "" - . , - , ( , - , ) . ( ), . Linux mmap (2) " ", VAS.

PPS thrashing ( ). - ( ). operator new malloc VAS ( ) (, VirtualAlloc Windows mmap sbrk Linux). Microsoft , virtual address space means something else that remains forever unchanged (but this is not very interesting and is not a feature of the process, but the hardware that AMD calls "virtual memory", see also this ).


NB. I do not know Windows and have never used it (and I hope never to write on it in my professional life). But I have been programming since 1974 and have been using Unix-like systems since 1987. Today I am a Linux user, and I know a bit about the internal components of the GCC compiler.

+2
source

Source: https://habr.com/ru/post/1695855/


All Articles