0

What are the downsides of using VirtualAlloc for arrays, reserving large chunk of address space, and committing pages when they are needed? For example if I know max cap for my array, I can reserve address space for it, using VirtualAlloc, and commit memory if I actually need it.

Unlike std::vector in this case, all pointers will remain valid after adding need pages to array.

I understand that this is not suitable for all arrays in program, but in the case when I need to save a pointers after array grows/resize?

Or should I just use std::deque? But with std::deque I gonna lose continues address space.

Heorhiy
  • 190
  • 2
  • 14
  • Undefined Behaviour unless the array contains [Implicit Lifetime Types](https://en.cppreference.com/w/cpp/named_req/ImplicitLifetimeType), or you use placement new in the newly committed space. See also [Object](https://en.cppreference.com/w/cpp/language/object). – Richard Critten Apr 04 '23 at 07:59
  • I can managed that ;) – Heorhiy Apr 04 '23 at 08:04
  • Why don't you try defining an allocator for `vector`. The hint version of `allocate` method may come in handy, if the specific implementation of vector actually uses it. I don't know if std demands that containers should use it when they can, but I guess implementors do not miss the optimization options this offers. – Red.Wave Apr 04 '23 at 17:42

1 Answers1

0

VirtualAlloc is a low-level Windows API that provides lots of options for memory allocation. It can be useful in specific situations, such as when you need to share memory directly with another process. However, it is not recommended for general-purpose memory allocation.

One downside of using VirtualAlloc for arrays is that it can only allocate memory in larger chunks. This means that if your address space becomes fragmented, you have no recourse but to release and rebuild. Additionally, VirtualAlloc manages pages in the Windows virtual memory system, while HeapAlloc allocates from a specific OS heap. You are unlikely to ever need to use either of them.

In contrast, std::vector automatically manages memory allocation and reallocation for you. It can grow dynamically as needed and all pointers remain valid after adding new pages to the array.

If you need to save pointers to elements in a container after it grows or resizes, std::deque could be a good option. Unlike std::vector, which stores its elements in a contiguous block of memory and may need to reallocate and move its elements when it grows, std::deque stores its elements in multiple blocks of memory. This means that when a std::deque grows, it can add new blocks of memory without invalidating pointers to its existing elements.

However, keep in mind that std::deque has some trade-offs compared to std::vector. For example, it may not have as fast random access to its elements and may use more memory due to the overhead of managing multiple blocks of memory.

Ayush Naik
  • 111
  • 2
  • 14
  • That's what I'm saying, VirtualAlloc solves downsides of deque, and keeps upsides of vector. Yes it's page granularity, but it is not problem for large arrays. And small arrays I can preallocate if needed anyway – Heorhiy Apr 04 '23 at 08:01
  • I see your point @Heorhiy `VirtualAlloc` does provide benefits of both vector and deque. If you are preallocating then I would say `VirtualAlloc` is best for your case. – Ayush Naik Apr 04 '23 at 08:09
  • `VirtualAlloc` is a Windows specific API, so if you need your code to be portable to other platforms, you may have to use a different memory allocation function like `vector` or `deque`. – Ayush Naik Apr 04 '23 at 08:12