tags:

views:

358

answers:

5

If I need a large buffer in my program written in C++, which one is better?

  1. Allocate the buffer in heap, and keep a reference to that buffer in the class that use it.

  2. Allocate a static buffer, and make it global.

+2  A: 

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

Sebastiaan Megens
+14  A: 

How about: 3. Use a vector.

[Edited to add: or boost::array is a good option if you're happy with the dependency]

Steve Jessop
That really doesn't answer the question - it's basically just suggesting 1), since a vector's just a heap allocation in a wrapped form.
Reed Copsey
@Reed: Yes it does. It means - use heap memory but avoid managing it yourself.
EFraim
Reed: I think this answer makes the OP to rethink about buffer management altogether, +1 for that.
Indeera
Sure, it directly answers the question in the title and implicitly advocates (1) over (2). But I don't want to say "do 1" in case that's taken to mean "call new and store the resulting pointer wherever".
Steve Jessop
+2  A: 

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.

Reed Copsey
+1  A: 

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.

Mike McQuaid
+1  A: 

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.

Chris Kaminski