Say for example I have a function that takes some argument and a size_t length
to initialize an array on stack inside a function.
Considering the following:
- Strictly the
length
can only be on the range of 1 to 30 (using a fixed max buffer length of 30 is not allowed). - The
array
only stays inside the function and is only used to compute a result.
int foo(/*some argument, ..., ... */ size_t length) {
uint64_t array[length];
int some_result = 0;
// some code that uses the array to compute something ...
return some_result;
}
In normal cases I would use an std::vector
, new
or *alloc
functions for this but... I'm trying to optimize since this said function is being repeatedly called through out the life time of the program, making the heap allocations a large overhead.
Initially using an array on stack with fixed size is the solution that I have come up with, but I cannot do this, for some reasons that I cannot tell since it would be rude.
Anyway I wonder If I can get away with this approach without encountering any problem in the future?