Data changes when working with classes When I pass an array, and it's len to the constructor of a class. I store the length, and access it later, it's value changes, seemingly at random. 

#include "Arduino.h"

#define NUMSENSORS 2
const byte dataPins[] = {A5, A4};

class B {
    public:
        B(byte pin) { _pin = pin; }
        byte getPin() { return _pin; }
    private:
        byte _pin;
};

class A {
    public:
        A(const byte sensorPins[], int len) {
            Serial.print("provided len is: ");
            Serial.println(len);
            _len = len;
            Serial.print("Stored len is: ");
            Serial.println(_len);
            for (int i=0; i&lt;len; i++) { myBarray[i] = new B(sensorPins[i]); }
        }
        B* myBarray[];
        int _len;
        void printLen() {
            Serial.print("Length is: ");
            Serial.println(_len);
        }
};

void setup() {
    Serial.begin(115200);
    A test(dataPins, NUMSENSORS);
    for (uint8_t i=0; i&lt;10; i++) {
        test.printLen();
        delay(100);
    }
}

void loop() {
}


The value of len is fine in the constructor,  but is different when I call it in the function "getLen".

I feel as though it has something to do with pointers. Because if I comment out the for loop in the constructor of A, the value is correct. 

Any help would be greatly appreciated, thanks in advance.
Let us do this with a template class and avoid malloc, etc.

#define membersof(x) (sizeof(x) / sizeof(x[0]))

const byte dataPins[] = { A5, A4 };

class B {
public:
  byte getPin() { return (_pin); }
  void setPin(byte pin) { _pin = pin; }
private:
  byte _pin;
};

template&lt;unsigned len&gt;
class A {
public:
  A(const byte sensorPins[]) {
    for (unsigned i = 0; i &lt; len; i++)
      _vec[i].setPin(sensorPins[i]);
  }
  unsigned getLen() { return (len); }
  B* getB(unsigned ix) { return (ix &lt; len ? &amp;_vec[ix] : NULL); }

private:
  B _vec[len];
};

void setup() {
  Serial.begin(115200);
  A&lt;membersof(dataPins)&gt; test(dataPins);
  unsigned len = test.getLen();
  Serial.print(F("test.getLen()="));
  Serial.println(len);
  for (unsigned i = 0; i &lt; len; i++) {
    B* bp = test.getB(i);
    Serial.print(F("test::B["));
    Serial.print(i);
    Serial.print(F("].getPin()="));
    Serial.println(bp-&gt;getPin());
  }
}

void loop() {
}


Cheers!