Pass array size constant from main file to class

I'd like to define TEST_ARRAY_LENGTH in my main file and setup an array of this size in my class.

Can't seem to get around having "float test_array[800];" hardcoded in my Test_class.h definition.

Main file

#include "Test_class.h"

#define TEST_ARRAY_LENGTH 800

Test_class Test_object(TEST_ARRAY_LENGTH);

void setup() {
}

void loop() {
}

Test_class.cpp

#include "Test_class.h"
#include "Arduino.h"

Test_class::Test_class(int TEST_ARRAY_LENGTH)
{
  test_array[TEST_ARRAY_LENGTH];
}

Test_class.h

#ifndef Test_class_h
#define Test_class_h
#include "Arduino.h"

class Test_class{
  public:
    Test_class(int TEST_ARRAY_LENGTH);
    float test_array[800];
};
#endif
#ifndef Test_class_h
#define Test_class_h
#include "Arduino.h"

class Test_class {
  public:
    Test_class(const int tar);
};
#endif
//CPP
#include "Test_class.h"
#include "Arduino.h"

Test_class::Test_class(int tar)
{
  float test_array[tar];
}

@kolaha
Nope, not going to work. A simple test in the Arduino IDE shows that.

@chris9
I can think of two choices ... use a template or allocate the array dynamically.

yup, not very useful class

what can i change to make it work? volatile? declare in H as public?

@kolaha
Yes, that's what I thought would work but unfortunately it didn't.

Could you please expand on this comment. I don't understand. There will be methods in the class that use the array, will it not exist for them?

Could you please expand a little on these ideas. An example perhaps. They sound like they might be viable solutions.

The template method gives you the most protection by the compiler against defining an array that's too large for your processor board.

Test_class.h:

#ifndef Test_class_h
#define Test_class_h
#include "Arduino.h"

template<size_t ARRAY_SIZE>
class TestClass {
  public:
    float test_array[ARRAY_SIZE];
};

#endif

Main .ino:

#include "Test_class.h"

TestClass<800> TestObject800;
TestClass<600> TestObject600;

void setup() {
}

void loop() {
}

Yes, but the OP wanted the array as part of his(her?) class. So it would have to be allocated dynamically as part of the constructor.

See Post #13.

There would be other methods that use the array. For example...

Main

#include "Test_class.h"
#define TEST_ARRAY_LENGTH 800
Test_class Test_object(TEST_ARRAY_LENGTH);

void setup() {
  Test_object.write_test_array(0,25.5);
}
void loop() {
}

Test_class.cpp

#include "Test_class.h"
#include "Arduino.h"

Test_class::Test_class(int TEST_ARRAY_LENGTH){
  test_array[TEST_ARRAY_LENGTH];
}

void Delay_class::write_test_array(int test_index, float test_value) {
  test_array[test_index] = test_value
}

Test.h

#ifndef Test_class_h
#define Test_class_h
#include "Arduino.h"

class Test_class{
  public:
    Test_class(int TEST_ARRAY_LENGTH);
    float test_array[800];
    write_test_array(int test_index, float test_value)
};
#endif

Ok. Thank you. Are we talking about " Class scope" here?
I will post an example of a method that might use the array.

That looks great! Might be exactly what I need. Thank you, this has all been very informative and helpful. Thanks.

Thank you for the feedback.

Could you expand on that. An example perhaps? I'm not sure I understand the difference between when it is Class scope (would have the array as a member of the class) and when the array is a local variable in a constructor (local scope to that constructor function).

Second option - dynamic array allocation.
Useful if array size unknown at compile time.

class TestClass {
  public:
    // contsructor with dynamic array creation
    TestClass(int array_size) {
      array_ptr = new float[array_size];  
    }
   // When using new(), destructor is mandatory
    ~TestClass() {
      delete[] array_ptr;
    }
    // pointer to array
    float* array_ptr = NULL;
};

// Create class instance with 100 elements array
TestClass tc = TestClass(100);

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Another good suggestion.

Thank you for the examples, they're really helpful.

This example violates the rule of three/five/zero and will inevitably cause heap corruption when copied or passed by value.

You should never use new/delete in high-level code. Memory management should be handled only by single-purpose classes such as std::unique_ptr or std::vector, allowing you to use the rule of zero for all higher-level code.


When using dynamic memory on a microcontroller, it is also important to be aware of the dangers of memory fragmentation.

Microcontrollers code is not high-level code (in most cases).

I agree with this, but if the lifetime of this class is from the start to the end of the program, then the danger of fragmentation is zero.

In general, I fully agree with the recommendations not to use dynamic memory allocation in microcontrollers, but there are situations when this is the most convenient option. The main thing is to understand what you are doing

Thanks for the link.
I know this, but I missed it. Yes, you must either define your own copy and assignment constructor, or disable them