I am writing a binary sorting algorithm (for training purposes). Compiler - MinGW-w64 with optimization flag -o0
The fact is that my code consumes more memory than I expect ..
Structure {key, value, pointer to the left and right ancestor} When using size_t types in the first and second values in the x64 system, it is logical to assume that the total size of the structure is 8 + 8 + 8 + 8 = 32 bytes. But in reality, when creating 100,000,000 Objects of the application does not eat 3.0GB and 4.5GB, which is significantly higher than expected.
I read about the alignment of memory and the use of #pragma pack (push, 1) in the global area does not affect the situation (which, incidentally, is logical, is a flat structure).
I also used an additional function to allocate memory through malloc, as I was told that the problem might be with new calls. Unfortunately, it also did not help, but at least made it possible to monitor the size of the allocated memory during execution .. From what I concluded that the size of the allocated memory is correct - 3,200,000,000 bytes, at least during the allocation phase.
That is, most likely it is still some kind of compiler optimization. However, when creating a simple array of 32 byte structures, the application consumes as much as needed. Therefore, it is a matter of the memory allocation logic (Insert (...) function)
The code I use is: size_t memCount = 0;
template<typename Type, typename... Args> Type* Alloc(Args... args) { uint8* result = reinterpret_cast<uint8*>(malloc(sizeof(Type))); memCount += sizeof(Type); if(result) return new(reinterpret_cast<Type*>(result)) Type(args...); return nullptr; } template<typename kType, typename Type> class Node { private: kType key; Type value; Node* left; Node* right; public: explicit Node(const kType& key, const Type& value) : key(key), value(value), left(nullptr), right(nullptr) { } ~Node() { if(left) delete left; if(right) delete right; } const bool Insert(const kType& key, const Type& value, const kType& min, const kType& max) { if(min == max) return false; kType halfMax = (max - min) / 2 + min; if(key < halfMax) { if(!left) { left = Alloc<Node>(key, value); return true; } else return left->Insert(key, value, min, halfMax); } else { if(!right) { right = Alloc<Node>(key, value); return true; } else return right->Insert(key, value, halfMax, max); } return true; } }; int main(void) { Node<size_t, size_t>* arr = Alloc<Node<size_t, size_t>>(0, 0); for(size_t x = 1; x < 100000000; x++) arr->Insert(x, x, numeric_limits<size_t>::min(), numeric_limits<size_t>::max()); cout << "mem - "<< memCount << endl; // здесь видим 3 200 000 000 (2,9гб), как и должно было быть system("pause"); return 0; };