4 ms·
Don't see what is incorrect. What you're talking about is taking advantage of a feature mostly unique to Linux (memory overcommit, not supported on Windows or m
by Kranar 3y ago
Don't see what is incorrect. What you're talking about is taking advantage of a feature mostly unique to Linux (memory overcommit, not supported on Windows or most other OSs) to construct a vector whose capacity is some really large value so as to never need to perform a reallocation for a given data set.
In practice it is very common for C++ developers to preallocate a region of memory upfront that is large enough to handle their dataset without having to go to such an extreme and non-portable length.
None of this contradicts the CS101 explanation, preallocating memory is entirely consistent with the fundamental understanding of how dynamic arrays work.
- malkia 3y agoOn windows you can do this with RESERVE, e.g. MEM_RESERVE from https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc https://learn.microsoft.com/en-us/windows/win32/api/memoryap... - For example when you have a callstack (on Windows), it can reserve (say) 1MB of contigous space, but then only "commit" some parts of it, and the tail (head?) be sort of guard page, in order to "commit" more from the reserve. Now getting commit from reserve might fail, as you've mentioned because Windows does not overcommit, but it's possible to a degree. I guess same with really large vector can be done.