1

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.

migrated from stackoverflow.com Jul 7 at 2:28

This question came from our site for professional and enthusiast programmers.

  • 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 here – P__J__ Jul 6 at 12:58
  • Arduino library doesn't need to be a class – Juraj Jul 7 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 Jul 7 at 11:15
1

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. – Nick Gammon Jul 7 at 11:17
  • 1
    a note: for every use of the template with a different size, compiler generates a different class. every class takes space in program memory – Juraj Jul 7 at 14:17
  • @NickGammon: Good point. Added the link. – Edgar Bonet Jul 7 at 14:44
  • Memory allocated using new[] has to be deleted using delete[], not delete. – tttapa Jul 7 at 22:56
  • tttapa is right. That line should read: delete [] theArrayOfNumbers; – Nick Gammon Jul 8 at 6:59
3

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.

0

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? – Juraj Jul 7 at 6:04
  • Really good points @Juraj, I'd add them but Edgar has already provided good examples. Thanks. – st2000 Jul 7 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.