c++ heap allocations 32 byte aligned by default? -


i decided test memory consumption visual studio 2013 community edition, , noticed on computer memory consumption skyrocketed when using pointers, example:

int _tmain(int argc, _tchar* argv[]) {     auto n = 1000000;      std::vector<int> numbers;     numbers.reserve(n);      (auto = 0; < n; i++){         numbers.emplace_back(0);     }      return 0; } 

i 4 bytes per int, total of 3.8mb, can corroborate via task manager.

but if decide change std::vector<int> std::vector<int*>:

int _tmain(int argc, _tchar* argv[]) {     auto n = 1000000;      std::vector<int*> numbers;     numbers.reserve(n);      (auto = 0; < n; i++){         numbers.emplace_back(new int(0));     }      return 0; } 

i original 3.8mb indicate pointers in computer 4 bytes, plus 30.5mb indicates every new int() used 32 bytes, incurring 28 bytes of overhead per int.

if change raw pointer std::unique_ptr uses same memory expected, changing std::shared_ptr (without using std::make_shared) adds 42mb because of pointer , variables.

all got me surprise, know not realistic example use many pointers primitives, since large applications tend use lot of pointers wondering if means should expected heap allocations use minimum of 32 bytes? or configurable somewhere on visual studio or platform dependent?

if so, there way debug , see how unused overhead our program has on heap? helpful in deciding when use custom allocation, example can fixed use 7.6mb instead of 34.3mb allocating memory beforehand , using placement new example.

thanks.

this expected. standard allocators typically guarantee level of alignment start of each allocation. allows mechanical sympathy around caching, , thing, aligned accesses execute faster unaligned ones.

also allocations msvc in debug builds typically include guard bytes before , after area allocated, increasing size significantly, small allocations yours.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -