I'm working on a group project to involving the creation of an in-house library using the Arduino IDE. As the Arduino IDE uses C++, the library must be a class. One of the classfields is an array of numbers. We would like it if we could specify the array length during object construction, as there are pros and cons to low and high array lengths. Is it possible to specify or change the array length during object construction?
Unfortunately, the Arduino IDE doesn't allow the usage of C++ vectors, so that's out of the question.
-
Rather rethink your idea and algorithm. Vectors are not used for a reason. Tiny uC require more consideration. Methods form the big machines usually are useless hereP__J__– P__J__2019年07月06日 12:58:32 +00:00Commented Jul 6, 2019 at 12:58
-
Arduino library doesn't need to be a classJuraj– Juraj ♦2019年07月07日 06:05:27 +00:00Commented Jul 7, 2019 at 6:05
-
It's not out of the question. There are various ports of the STL (Standard Template Library) for the Arduino. Try searching for them. The Arduino IDE does not itself preclude using them.Nick Gammon– Nick Gammon ♦2019年07月07日 11:15:36 +00:00Commented Jul 7, 2019 at 11:15
3 Answers 3
As suggested in st2000’s answer, you could use dynamic allocation but, if you do that, do not forget to free the allocated memory in the destructor. Otherwise you have a nasty memory leak and your program will not live long:
class MyClass {
public:
MyClass(size_t length) : arrayLength(length) {
theArrayOfNumbers = new int[arrayLength];
}
~MyClass() {
delete[] theArrayOfNumbers;
}
private:
const size_t arrayLength;
int *theArrayOfNumbers;
};
One issue with this approach is that dynamic allocation is no very friendly to the limited amount of memory you have in a microcontroller.
A safer option would be to use a template class instead. This way the array is statically allocated:
template <size_t arrayLength>
class MyTemplateClass {
int theArrayOfNumbers[arrayLength];
};
One caveat with this approach is that the users of the library may not be comfortable with the syntax for instantiating a template. You will have to provide clear examples in the documentation. Another issue is that the array size will have to be a compile-time constant.
-
As suggested in the previous answer - better to link to the answer. There is no such thing as permanent "previous" in Stack Exchange. The order of answers depends on the voting. Each answer has a share link which can be used to get a suitable link to it.2019年07月07日 11:17:32 +00:00Commented Jul 7, 2019 at 11:17
-
1a note: for every use of the template with a different size, compiler generates a different class. every class takes space in program memory2019年07月07日 14:17:54 +00:00Commented Jul 7, 2019 at 14:17
-
@NickGammon: Good point. Added the link.Edgar Bonet– Edgar Bonet2019年07月07日 14:44:43 +00:00Commented Jul 7, 2019 at 14:44
-
Memory allocated using
new[]
has to be deleted usingdelete[]
, notdelete
.tttapa– tttapa2019年07月07日 22:56:53 +00:00Commented Jul 7, 2019 at 22:56 -
tttapa is right. That line should read:
delete [] theArrayOfNumbers;
2019年07月08日 06:59:54 +00:00Commented Jul 8, 2019 at 6:59
Another way is for the caller to provide the memory as a pointer to enough bytes for 'N' objects (cast, if necessary, as a pointer to the required type), and the number, 'N' of such objects. This gives the caller the choice of whether to allocate statically or dynamically.
It looks like you want an array of ints. So you'd allocate an array (statically, in my example, but you could choose whichever best fits your application):
int memArray[N];
// ...
myClass(N, memArray&);
I avoid allocating memory from the heap, but someone else might have a different preference or a situation that requires it; this technique lets the library work for either.
Some example solutions can be found in this stackexchange question/answer. The accepted answer used this code:
private int size;
private String words[];
public WordStore(int n){
words =new String[n];
}
-
and releasing the allocated memory? and prevent fast memory fragmentation?2019年07月07日 06:04:44 +00:00Commented Jul 7, 2019 at 6:04
-
Really good points @Juraj, I'd add them but Edgar has already provided good examples. Thanks.st2000– st20002019年07月07日 14:01:43 +00:00Commented Jul 7, 2019 at 14:01