Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt at dynamic memory allocation c++ [closed]

Tags:

c++

winapi

Im trying to allocate preciesly 1 KiB of memory by the usage of 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;

problem is everytime i run this code it seems to allocate anywhere between 100 KiB to 400 KiB i used char since it's 1 byte in size

like image 986
jakeyMcDonlad Avatar asked Dec 08 '22 15:12

jakeyMcDonlad


1 Answers

The implementation is free to allocate more than you asked for behind your back in order to be able to satisfy future (expected) allocations faster (without having to involve the OS). This is usually a good optimization and nothing to worry about - especially since operating systems may not even back that allcation with actual physical pages before you actually write to them, so it often costs nothing at all.

In any case, from a C++ language perspective; as long as you got the memory you requested, whether or not the implementation actually allocated more is not usually something you can know nor something you should care about.

like image 183
Jesper Juhl Avatar answered Dec 10 '22 05:12

Jesper Juhl