Can't access array of pointers inside class?

Hello all.
I'm writing a scheduler program for the ESP-01 chip. So far I have a Task class with this structure:

class Task {
public:
  void (*callback)();
  int started;
  int length;
  Task(){};
  Task(int x, void (*y)()) {
    started = millis();
    length = x;
    callback = y;
  }
};

This takes a callback and the amount of time from creation to call it. I'm now trying to make a TaskList class, that will take an array of Tasks and the length of that array. Then, you can call the check() function inside it to check all of those Tasks and see if they're done yet.

class TaskList {
public:
  int length;
  Task **tasks;
  void check() {
    Serial.println(this->tasks[0]->length); // this is what always fails
  };
  TaskList(){};
  TaskList(Task *x[], int y) {
    tasks = x;
    length = y;
  }
};

The code compiles without errors, but the device crashes every time I try to read data from tasks inside or outside the TaskList class. I'm new to C++, and very bad with pointers and classes. If somebody could help me out, that would be great.

Thanks in advance.

Hello

Show full code

Full code here:

void setup() {
  Serial.begin(115200);
  Serial.println();

  // set up Tasks and list
  Task one(3000, asdf);
  Task two(6000, asdf);
  Task three(4000, asdf);
  Task *x[] = {&one, &two, &three};
  TaskList z(x, 3);

  z.check();  // this will fail
  Serial.println(z.tasks[0]->length);  // this will also fail
}

class Task {
public:
  void (*callback)();
  int started;
  int length;
  Task(){};
  Task(int x, void (*y)()) {
    started = millis();
    length = x;
    callback = y;
  }
};

class TaskList {
public:
  int length;
  Task **tasks;
  void check() {
    Serial.println(this->tasks[0]->length); // this is what always fails
  };
  TaskList(){};
  TaskList(Task *x[], int y) {
    tasks = x;
    length = y;
  }
};

You know what, I did try your classes even before I asked you to show code, it worked as expected (at least, on arduino nano). But you still didn't provide your full code, as asdf is missing

From what I see you have two problems:

  1. you are confusing pointers and arrays in one, this results in low readability code, in your case it looks like you would like to use vectors, smart pointers (maybe) and the function object.

  2. You are making global pointers to local variables on the stack, this will result in undefined behavior the moment you exit the setup() function, use 'new' or define your variables globally.

** move your classes to separate header files, and use privatize your class members

That's very strange. I doubt it's an issue of compatibility for the ESP8266....

As for the asdf function, it's just a void function that prints a message to the serial monitor.

Would you mind providing some sample code of what you would recommend? As i said before, I'm very new to low(ish)-level programming.

TaskList tasks = {
new Task([](){/*function 1*/}),
new Task([](){/*function 2*/})
};
 
void setup()
{
}

void loop(){
tasks.execute(/*here you could supply additional arguments for your functions*/);
}

In general, if you are new to C++, it is one of the worst things you can do to yourself if you start with function pointers.

You are making global pointers to local variables on the stack

That's the big thing.

To be less dramatically different that flyingjimbo's example (lambdas? Really?), I think something like this should work better:

vo;id setup() {
//  
  // set up Tasks and list
  static Task one(3000, asdf);
  static Task two(6000, asdf);
  static Task three(4000, asdf);
  static Task *x[] = {&one, &two, &three};
  TaskList z(x, 3);

[/quote]
You could use "new" instead of "static" if you trust dynamic allocation.

Full code here:

Wasn't. Didn't compile without changes. (relatively obvious changes, but still...)