Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::vector might be overkill #41

Open
Poofjunior opened this issue Jul 2, 2014 · 1 comment
Open

std::vector might be overkill #41

Poofjunior opened this issue Jul 2, 2014 · 1 comment
Assignees

Comments

@Poofjunior
Copy link
Collaborator

The bosch hardware interface has been rewritten at the top-level to accept std::vector<uint8_t> references instead of a pointer to an array and the size of that array. While this works and shortens the number of arguments, vectors are designed to be resized dynamically. Since we know exactly how many bytes we want to read from the sensor, and since that number is fixed at compile time, we can simply use a std::array to promote readability. The top-level reader of the bosch_hardware_interface header file doesn't know this, but we have to call the size() member function before reading out the number of bytes in the driver code. Switching over to two arguments: an array pointer and a uint8_t specifying how many bytes to read/write will make compiling the code for bare-metal platforms like the Arduino much easier since we wont have to find or write a basic std::vector implementation for Arduino.

To use the code that currently uses std::vectors on the Arduino will require that we find an implementation of std::vectors for Arduino. Writers of a std::vector implementation on Arduino will need to implement dynamic resizing for the implementation to be a proper port to the Arduino. Since we don't use this feature, we may be able to switch back to array pointers to be able to implement the driver both on the pc and directly on the Arduino.

Lastly, I did a quick size check for vectors vs fixed-size arrays. Two simple programs: one creates a fixed size array of uint8_ts of size 4 and the other creates a std::vector of uint8_ts of size 4. The fixed size array is 8.3kB and the std::vector is 15.4kB.

@proan proan self-assigned this Jul 3, 2014
@proan
Copy link
Member

proan commented Jul 3, 2014

I agree that std::vector is overkill, and you've correctly identified the issues with it. I used std::vector in place of std::array because it's better know, but I wholly agree with you that we should change to the fixed size of std::array.

The two main reasons for moving away from uint8_t* and a byte count are to remove issues with pointing to the wrong place and to remove having to set the size of the array manually. Buffer overruns will definitely occur if the programmer has to pass the size of the array along with a pointer to it.

I will change std::vector to std::array, and we can work on implementing that in Arduino.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants