0

I want to pass an array which can have different sizes to a function. I am on an embedded system with limited capacities. I thought about the following ways of implementing it:

  1. I don't want to use templates, because the function is called with multiple different array sizes. Every call would create a new set of this function, which would unnecessarily blow up my project.
  2. Using a simple pointer + length variable seems to be inelegant and unsafe. Also I couldn't nicely iterate over such a construct.
  3. Chained lists seems to be well overcomplicated for this problem.
  4. I don't want to use vectors, because they can not be stored statically. So maybe there could be a time, when not enough heap is available for my vector. The firmware needs to be as reliable as possible.

Is there a way of accomplishing this? Are my thoughts about the systems reliability too cautious?


Edit

  1. Span would be a nice possibility in my opinion. But, In my case I don't have C++20 support.
  2. Wrapper function for the template as @Some programmer dude suggested would also be an option! An advantage against a "normal" template function would be, that the function itself can still be in the source and doesn't need to be in the header file.
19
  • 1
    I think you've eliminated all the viable and preferralbe options. Commented Aug 8, 2024 at 8:48
  • C++20 allows skipping the size of the array when the array is passed by reference or pointer. int a[3]{};int b[4]{};void func(int (&ref)[]){ref[0] = 4;}int main(){func(a);func(b);} is valid C++. See demo Commented Aug 8, 2024 at 8:48
  • 4
    std::span maybe? Commented Aug 8, 2024 at 8:49
  • 6
    And please edit your question to tell us why you have these (seemingly) arbitrary limitations What is your actual task or assignment? What are the actual limitations (and requirements) of that task or assignment? Commented Aug 8, 2024 at 8:56
  • 1
    You can use option 2 with en.cppreference.com/w/cpp/container/span Commented Aug 8, 2024 at 9:01

1 Answer 1

4

Most C++ algorithms operate on a [begin, end) pair. In case of a continuous array one uses pointers to the first element and behind the last, which allows for efficient iteration:

float data[SZ];
my_function(data, data+SZ);
//...

void my_function(float* begin, float* end){
  for(float* cur = begin; cur != end; ++cur){
    //do something with *cur
  }
}

Using a simple pointer + length variable seems to be inelegant and unsafe.

In this case, safety is a concern but not more than any other code where you address an array directly (indices of a vector may go out of bounds as well, linked list may lead to dereferencing a null pointer, etc). C++20 provides std::span, a syntax sugar for this approach:

float data[SZ];
std::span<float> better_data(data);
my_function(better_data); //you can simply call my_function(data) 
                          //for implicit span construction
//...

void my_function(std::span<float> seq){
  for(float& val : seq){
    //do something with val
  }
}

(Notice that std::span can store static information about an array size but it would run counter to your desire to have one function for all array sizes.)

Sign up to request clarification or add additional context in comments.

7 Comments

You don't need an intermediate span, you can pass data directly to my_function.
True, but I wanted to show span creation explicitly. Added a comment.
The use of std::begin() and std::end() (or rather using std::begin, std::end; f(begin(array), end(array)); is a big plus for code genericity.
One could also add a function template for the first version for convenience. Demo. That won't generate any extra code bloat in optimized builds.
begin/end-parameter could be a solution, but they are still prone to errors during function call. Calling a function with just the array/span/vector/etc. is much safer, than needing two parameters. So maybe this in combination with a template class, which wraps the array-call to a call with begin/end-pointer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.