1

For context, I'm working in c++ on a stm32f ucontroller doing embedded audio and trying to avoid dynamic allocation.

One of the classes I've written is a wavetable oscillator that is, for the scope of my question, essentially just a bunch of c-style arrays.
I have these files, among others, in a separate folder from the specific project that is using them so I can reuse them across projects with no rewriting or copy/pasting.

The values that fill the arrays are calculated during an initialization routine rather than being hardcoded so that, if I change the "#define tableLength 256" statement at the top that sets the size of the arrays from 256 to some other value, the waveforms will be recalculated properly the next time I compile and flash the program onto the chip.

Since I'm working with limited memory resources, some programs that use this wavetable oscillator may necessitate that these tables be of a smaller size.

Is there a way of specifying the size of these arrays from a given project's main program file so that nothing needs to be changed in the shared wavetable oscillator file?

I'm still pretty early into my programming journey, but my intuition tells me there must be some sort of mechanism to accomplish this since the size of the arrays will still be known at compile time?

I'm trying to avoid changing the array size directly in the shared file, because any file that gets modified since the last flashing gets recompiled during the flashing process.
This means I would have to remember to change the value every time I needed to flash a different product or work on a different project.

For further context, I'm building and uploading the programs using a makefile.
My knowledge of using them is very minimal, so maybe there's a way to specify the size to be used for the arrays from the project's makefile?

Thanks for the help.

noif
  • 13
  • 3
  • You cannot be working in "C/C++" because there is no such language. It sounds like you are using C++ (you have classes, and you refer to "c-style" arrays) so I have removed the [c] tag. – John Bollinger Dec 17 '20 at 03:19

1 Answers1

0

Since you include the c++ tag in your question and c++ language includes templates, then you can parametrize the size of a static array declared in another file from a MAIN.cpp file like this:

In MYCLASS.cpp (declared in MYCLASS.h)

template <unsigned int BYTES>
class CBase
{
public:
    char Arr[BYTES];

    int Fn1(void) {
        return Arr[1] ^ Arr[sizeof(Arr)-1];
    }

    int Fn2(void) {
        return Arr[2] ^ Arr[sizeof(Arr)-2];
    }
};

In MAIN.cpp:

#include <stdio.h>
#include "MYCLASS.h"

int main(void)
{
    CBase<32> ddd;  //The number 32 causes the CBase to be instantiated with a 32-byte static array Arr[32].

    printf("%d\n", ddd.Fn1()); 
    printf("%d\n", ddd.Fn2());

    return (int)ddd.Arr[0];
}

Instead of the number 32 in MAIN.cpp, you can use a preprocesor #define, which can be taken from a makefile if you wish.

Note: If you plan to inherit from the CBase class then be prepared to run into this problem.

George Robinson
  • 1,500
  • 9
  • 21