Dynamic instantiation of objects?

Hey folks, got a really newbie question here. I am working with the Servo library, and was wondering if there is a way for me to dynamically instantiate Servo objects.

In most examples, I see static instantiation of Servo objets, like this:

Servo myServo;

Which is cool when you know how many servos you're going to have. But is there a way for me to instantiate a number of Servo objects based on a variable, such as (I'm using C++ syntax here):

Servo arrayOfServos[MAX_SERVOS];
if (numServos < MAX_SERVOS)
{
for (int i = 0; i < numServos; i++)
{
Servo newServo = new Servo();
newServo.attach(somePin);
newServo.write(0);
arrayOfServos = newServo;

  • }*
    }
    Any help will be appreciated here!
    Thanks,
    - K.

Which is cool when you know how many servos you're going to have. But is there a way for me to instantiate a number of Servo objects based on a variable, such as (I'm using C++ syntax here):

You could, but, really, does that make sense?

The new operator returns a pointer of the appropriate type, so your example is incorrect.

The real question is, aside from just randomly waving servos around, what practical purpose is there in not knowing about the number of servos in advance?

You can create objects dynamically, using pointers to refer to them subsequently, but that doesn't seem to be what you need here.

I think you just need to define an array of servos and then attach each servo to the associated pin.

// incomplete, untested
Servo servo[MAX_SERVOS];
const byte SERVO_PIN[MAX_SERVOS] = { 2, 3, 4, 5, 6}; // etc
for (int i = 0; i < MAX_SERVOS; i++)
{
    servo[i].attach(SERVO_PIN[i]);
}

The syntax will be a bit different:

Servo *arrayOfServos[MAX_SERVOS];
if (numServos < MAX_SERVOS)
{
  for (int i = 0; i < numServos; i++)
  {
    Servo *newServo = new Servo();
    newServo->attach(somePin);
    newServo->write(0);
    arrayOfServos[i] = newServo;
  }
}

You can allocate new object this way. Unfortunately there is an error in the free() function of the C library in current IDEs resulting in a memory leak, so if you allocate and delete this objects often you probably run out of memory relatively soon. The Arduino has no memory management unit as todays PCs all have so even without that bug you have to take care to not fragment your memory too fast with such operations.

And as PeterH pointed out, the code as you presented it, doesn't need dynamical allocation.

1 Like
Servo arrayOfServos[MAX_SERVOS];
if (numServos < MAX_SERVOS)

Besides, you're potentially wasting one object's worth.

1 Like

Hey folks, thanks for the replies. Apologies for the syntax errors - I switched over to C# for a few years and haven't written C/C++ in a while.

I was actually not aware that the compiler supports pointers as I didn't notice it being mentioned in the Arduino reference. What I was (more or less) trying to support was this:

void AssignServosToPins(int numServos, int pinAssignments)
{
int i;
Servo **servoArray = (Servo **)malloc(numServos * sizeof(Servo ));
for (i = 0 ; i < numServos; i++)
{
servoArray = new Servo();
servoArray_->attach(pinAssignments);

servoArray*->write(0);
}
}*

I'm not too worried about the free() bug as this will be a one-time initialization only. As I mentioned, I don't know the capabilities of the compiler too well, so I was not sure if malloc(), sizeof() and the "pointer-to-pointer" double inference syntax are properly supported in the compiler.
Appreciate everyone's help. I guess I'll just have to do some trial-and-error._

1 Like

I guess I'll just have to do some trial-and-error.

Good idea. Just don't try to reference servoArray outside of that function.

1 Like