3 ms·
I’ll have to admit that I don’t fully know how these things work, but my impression was that a malloc(4GB) on most systems will not actually allocate 4GB, but o
by judofyr 5y ago
I’ll have to admit that I don’t fully know how these things work, but my impression was that a malloc(4GB) on most systems will not actually allocate 4GB, but only acquire virtual memory and the OS will give it memory when it’s being used.
Is this too simplistic and this package is doing something else?
- woodruffw 5y agoI haven't looked at this implementation yet, but your understanding is correct: reserving 4GB up-front with `std::vector` should only acquire virtual memory and not require an actual commitment, unless `std::vector` is doing something nuts internally like default-constructing each element that's been reserved. But I don't believe it does that.
- tom_ 5y agoI think you've got two sensible options in this case: commit, or do nothing. The standard's wording accommodates both: it's valid for a reserve to be a no-op, a situation the caller can detect (if they want to - assuming they even imagined anybody would ever do this! - I certainly never did, until now) by checking the new capacity. It's not obvious to me that it buys you much to reserve without committing. You're taking the hint, then ignoring it. Why bother? Why not just do nothing?r