Help: What is wrong with a clas global instance?

Hi am tiring to use a class instance with “Arduino Uno”, but it seems that the class initialization is lost in the program loop.

class CommandManager
{
private:
int* CommandList;

public:
void InitCommands(int* commandList)
{
CommandList = commandList;
}

void ExecuteCommand()
{
for (int i = 0; i < 2; i++)
Serial.println(CommandList*);*

  • }*
    };
    CommandManager cm;
    void setup() {
  • Serial.begin(9600);*
  • //---------- Init ------------*
  • int myCommands[2];*
  • myCommands[0] = 1;*
  • myCommands[1] = 2;*
  • cm.InitCommands(myCommands);*
  • //---------- Exec 1 ----------*
  • delay(3000);*
  • cm.ExecuteCommand();*
    }
    void loop() {
  • //---------- Exec 2 ----------*
  • delay(3000);*
  • cm.ExecuteCommand();*
    }
    The Exec 1 shows the correct values (1 and 2), but the Exec 2 shows random values. What is the reason for that behavior?
    Regards

I don’t suppose that your code is meant to be largely in italics with no index on the array. Did you read this before posting a programming question and follow the advice ?

No

Hi am tiring to use a class instance with “Arduino Uno”, but it seems that the class initialization is lost in the program loop.

class CommandManager
{
  private:
    int* CommandList;

  public:
    void InitCommands(int* commandList)
    {
      CommandList = commandList;
    }

    void ExecuteCommand()
    {
      for (int i = 0; i < 2; i++)
        Serial.println(CommandList);
    }
};

CommandManager cm;

void setup() {
  Serial.begin(9600);

  //---------- Init ------------
  int myCommands[2];
  myCommands[0] = 1;
  myCommands[1] = 2;
  cm.InitCommands(myCommands);

  //---------- Exec 1 ----------
  delay(3000);
  cm.ExecuteCommand();
}

void loop() {
  //---------- Exec 2 ----------
  delay(3000);
  cm.ExecuteCommand();
}

The Exec 1 shows the correct values (1 and 2), but the Exec 2 shows random values. What is the reason for that behavior?

Regards

Please excuse me UKHeliBob It is difficult to read everything when you just starting with new forum and community and asking for help.

That's because when you do this in your class

void InitCommands(int* commandList)
    {
      CommandList = commandList;
    }

you are not copying the data into the instance, you just save a pointer to the memory of the myCommands array which is declared as a local variable into the setup(). so when you exit setup(), the memory for myCommands is freed but you kept a pointer to that space in your class.

when you arrive in the loop() and try to print the data at that space, you get whatever is at that point in the old memory.

if you want to retain the data in the class, then you need to duplicate it there.

Short answer - scope

Its a classic error in C / C++ to pass a pointer to a variable out of its scope - there are no closures in C so
the variable pointed no longer exists, the pointer points to memory that will be recycled by other function calls in
the future.

Thank you J-M-L. You was absolutely clear! Obviously the C# have broken my C++ skills :slight_smile:
I will declare the "myCommands" array outside the "setup()" and that will fix my problem :slight_smile:

MarkT, AWOL thank you I will refresh my C/C++ knowledge :slight_smile:

Regards

Rotor_:
I will declare the "myCommands" array outside the "setup()" and that will fix my problem :slight_smile:

yes it will but then your class is of very limited interest because it can only point to global variables which will never be freed. I would suggest to copy the data inside your instance, this way when you free the instance, you free the memory. There is a side benefit that no-one then is messing around with that data as it can be hidden in the object.

J-M-L:
yes it will but then your class is of very limited interest because it can only point to global variables which will never be freed. I would suggest to copy the data inside your instance, this way when you free the instance, you free the memory. There is a side benefit that no-one then is missing around with that data as it can be hidden in the object.

J-M-L You are right I understand your points but this class was just simplification of my actual class that I hope that I will transform in a library soon.

  1. This class will contains list of function pointers and their identifiers (both encapsulated in a structure). That list will never be destructed. My main target was to give chance on the library user to decide how many functions to declare without unnecessary memory usage.

  2. Yes the encapsulation is important for the code quality but as I have mentioned above the last 10 years I have use C# (without touch to C++) and I don't have more cleaver idea how to implement "dynamic array" - With other words the library user to decide how long to be the list without memory overhead...

Regards

OK good - as long as you understand the pros and cons (and it clearly seems you do) then go for what makes the more sense indeed