Assuming a need of a buffer with fixed size, is there a size limit or threshold, such that under that size limit it's OK to use a fast stack-allocated std::array
, and above that limit it's better to use a std::vector
with memory allocated dynamically from the heap (since stack memory is precious and shouldn't be consumed much)?
// I think allocating 32 bytes on the stack is just fine.
std::array<BYTE, 32> smallBuffer;
// For 32KB, it's better getting memory from the heap.
std::vector<BYTE> bigBuffer(32*1024);
// Is it better to allocate a 1KB buffer on the stack (with std::array)
// or is it too much, and it's better to allocate on the heap (using std::vector)?
// What about 512 bytes? And 4KB?
// Is there a suggested size threshold?
std::array<BYTE, 1024> buffer;