Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporary buffer: Local or member variable?

Tags:

c++

In my class, I have one method which requires a large temporary buffer. I see two ways to implement this:

  • The buffer is a local variable and allocated statically on every call of the function:

    double buf[len];

  • The buffer is a member variable of the class and allocates dynamic memory (On construction or first call)

    this -> buf = new double[len]

The first way needs allocation of memory on each call, however, I expect stack allocation to be very fast. The second way defines an additional member variable, which is neither needed by other methods nor persistent between method calls, and therefore does not need to be a member variable.

Which one should I prefer?

like image 621
Michael Avatar asked Dec 03 '25 01:12

Michael


2 Answers

What options you have depends on a few questions:

  1. Is the function (potentially indirectly) recursive?

  2. Is len known at compile time?

  3. Can the function be called by multiple threads concurrently? On the same object, on different objects, not at all?

If the function is not recursive, you can use a static local variable. If len is a compile-time constant, static double buf[len]; will do. For a run-time value of len, use static std::vector<double> buf(len); instead. If concurrent calls are possible, add thread_local to the declaration.

For a recursive function, you have to use a non-static local variable. If len is known at compile time, this will certainly be very fast (it's just stack manipulation). If it's a runtime value, you'll either have to accept dynamic allocation on each call (e.g. std::vector<double> buf(len);), or create a memory pool of some sort.

like image 156
Angew is no longer proud of SO Avatar answered Dec 05 '25 15:12

Angew is no longer proud of SO


There's a third option, which is to use std::vector with static storage:

static std::vector<double> buf( len );

This doesn't require len to be a compile time constant and IIRC C++11 guarantees that the initialization of buf is thread-safe (in C++03 it wasn't guaranteed though). Of course, actually using the buffer won't be thread-safe: you'll either need some sort of synchronization mechanism (e.g. a mutex) or allocate the vector using thread-local storage (e.g. on the stack or via some TLS implementation).

like image 27
Frerich Raabe Avatar answered Dec 05 '25 15:12

Frerich Raabe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!