Instantiate array after declaration

Hello all,

I'm fairly new to C/C++, so this might be a trivial question, but I haven't been able to find a proper solution.

How do I instantiate (and declare the length) an array after having it declared before that? I currently have to create a global array, but it only gets instantiated in the constructor. Also, the length of this array depends on a parameter of the constructor, so I don't know the length when declaring.

This is my code:

Motor.h

#ifndef Motor_h
#define Motor_h

#include "WProgram.h"
#include <Servo.h>

class Motor {

      public:
            Motor(int ports[]);
            void setSpeed(int speed);
            
            int count;
            Servo motor[];

};

#endif

Motor.cpp

#include "WProgram.h"
#include "Motor.h"

Motor::Motor(int ports[]) {
      
      // Instantiate motors
      this->count = sizeof(ports) / sizeof(ports[0]);
              
              // Somehow instantiate the array?
        
      for ( int i = 0; i < this->count; ++i )
            this->motor[i].attach(ports[i]);
      
      // Initialize motors
      this->setSpeed(179);
      delay(1000);
      this->setSpeed(0);
      delay(1000);
}

void Motor::setSpeed(int speed) {
        for ( int i = 0; i < this->count; ++i )
              this->motor[i].write(speed);
        
        // Give motor some time to adjust
        delay(15);
}

You mean malloc?

First off, using this-> in front of fields or members of a class generally indicates one of two things. It means either that you have member and non-member fields of the same name, which is not a good thing, or that you don't understand the implicit this object, which is not a good thing.

If your class is named Motor, the assumption is that it does something with a single motor. Your Motor class contains an array of Servo objects. The array is named motor.

It looks like you want your class to control a collection, of indeterminate size, of Servo objects.

Perhaps you should start with an explanation of what an object of the class is going to be doing, and why it is to deal with a collection of Servos, where the number of servos is indefinite.

I read about malloc, and played around with it a bit, but I can't seem to get that right. I think I can calculate the right memory size with

sizeof(Servo) * this->count

but what return type should I give malloc and how do I truly instantiate the servo instances inside the array?

Sorry for the double post, I hadn t read PaulS s post before my previous post.

I generally use this-> to separate the parameters from the member fields, because they often have the same name, and it always looks silly to me to give them prefixes. Dunno, seems neater to me.

The purpose of the class is to control a collection of motors (so yes, the class/array name should probably be motors). I know the amount of motors I'm going to use (four), but I like to design my classes a little more independent of what I'm currently trying to achieve, so I'd like the class to be able to handle an indefinite amount of motors, since it really shouldn't be that hard. Now I'm mostly intrigued, and just really interested in how this generally can be resolved :smiley:

You can't give a return type to malloc - it already has one.
You could cast the value it returns to your datatype.

Yeah I know, void* if I remember correctly, but how do I cast it so that I can use that space for my Servo array?

What you really want to do requires the new method, which the Arduino does not provide. There are some forum posts that explain how to implement a new (and delete) method.

An alternative though, is to #define the number of motors.

#define MOTOR_COUNT 4

You'd then use MOTOR_COUNT like so:

Servo motors[MOTOR_COUNT];

Since it's unlikely that the number of motors changes dynamically, this save a lot of headaches with dynamic memory allocation and management.