If I need a large buffer in my program written in C++, which one is better?
Allocate the buffer in heap, and keep a reference to that buffer in the class that use it.
Allocate a static buffer, and make it global.
If I need a large buffer in my program written in C++, which one is better?
Allocate the buffer in heap, and keep a reference to that buffer in the class that use it.
Allocate a static buffer, and make it global.
The disadvantage with a static buffer is that you never exactly know when it will be deleted, if you want to use that buffer during destruction of some object, it could already be gone. So for more control, I would choose option 1.
Regards,
Sebastiaan
How about: 3. Use a vector.
[Edited to add: or boost::array is a good option if you're happy with the dependency]
I prefer heap allocation, for many reasons -
The main reason I prefer it is that it gives you a way, at runtime, to verify that the allocation was successful. When you attempt to allocate the memory, it will be obvious if it fails for whatever reason, and you can handle that much more gracefully than if you do a static buffer.
It also allows you to allocate various size buffers, and reallocate/free if possible, at a later time.
What does your program do? If the buffer lasts as long as the application and you know the size of it at compile-time then feel free to use a static buffer.
Also, as already mentioned, consider using a vector (unless you have benchmarks to show that the performance is too poor) to prevent buffer overflows.
When dealing with large amounts of memory, you have to look at two things, lifecycle, how often you create and recreate this buffer. As memory gets fragmented, there can come a time when you try to allocate a buffer of say, 512MB, and you can't, because your allocator cannot find 512MB of contiguous address space. This is why @onebyone's idea of using vectors is sometimes better. If you can reduce your footprint to byte-sized (not literally) chunks, you gain flexibility in how you manage your memory.
That said, I would almost NEVER recommend keeping a large static buffer around. It's asking for trouble.