Declaration of a Dynamic Array of Objects

15 posts in 24 hours! Thanks for all the help.

Personally, I have a Java background, but I'm pretty familiar with C's malloc.

I initially avoided malloc because many C++ "tutorials" suggest staying away from malloc as it is often a source for memory leaks. However, if it's the only way to truly dynamically allocate memory, it shouldn't be an issue.

After digesting InvalidApple's code and the responses generated I have concluded that malloc must call the default constructor for MyClass (without the int parameter).

#define ROWS 3
class MyClass {
  int _var;
  public:
    MyClass() {
      _var = 0;
    };
    MyClass(int a) {
      _var = a;
    };
    void setVar(int a) {
      _var = a;
    }
    int getVar() {
      return _var;
    };
};

MyClass *arr;
void setup() {
  Serial.begin(9600);
  arr = (MyClass *) malloc(sizeof(MyClass) * ROWS);
  for(int i = 0; i < ROWS; i++)
    arr[i].setVar(i + 1);
  
  for(int i = 0; i < ROWS; i++)
    Serial.print(arr[i].getVar());
    //Should return '1 2 3'
}

void loop() {}

Currently I don't have an Arduino to test this on, but I'm pretty sure this works. Multidimensional arrays would just require more 'for' loops.

Now for the real question:
Are objects extremely memory intensive (for an Arduino)? I've created a simple pixel class that lets me easily set the color of an RGB LED and I plan to create up to 64 of these 'Pixel' objects. Are there more efficient structures for this type of application?

Thanks again!