Dynamic instantiation of objects?

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