constructor problem

Hi everyone!
I have searched in the forum but i haven't found any solution to my problem.

The sketch has to store sensor variables like sensor address, output register address, setting register address. The first is scalar and last two are arrays.
I have created a class SENSOR and I have the constructor that receive 3 parameters: 1 numeric variable and 2 arrays.
The constructor store variables in private _variable in order to reach them easily for i2c communication (for example for the magnetometer device address is mag._device_address).
The problem is that when I loop the "for" in the constructor the second array is copied two times and shifted!

.h file:

#ifndef SENSOR_h
#define SENSOR_h

#include "Arduino.h" 
#include "Wire.h"


class SENSOR
{
  public:
   
    SENSOR(int device_address, int setting_array[], int output_reg_array[]);
    
    int _device_address;
    int _setting_array[];
    int _output_reg_array[];        //        MSB LSB
   

};

#endif

.cpp file:

#include "Sensor.h" 

SENSOR::SENSOR(int device_address, int setting_array[], int output_reg_array[])
  {

    _device_address = device_address;


        
      
    _device_address = device_address;
 
      for(uint8_t i=0; i<6; i++)
      {
    _setting_array[i] = setting_array[i]; 
                                                                         
      } 

       for(uint8_t i=0; i<6; i++)
      {
    _output_reg_array[i] = output_reg_array[i]; 
      };

.ino file:

#include "Sensor.h"

int mag_setting[]={0x00,16,0x01,32,0x02,0}; 
int mag_output_reg[]={0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; //    MSB/LSB/MSB/LSB/MSB/LSB  
int mag_address = 0x1E;


SENSOR mag(mag_address, mag_setting, mag_output_reg);

void setup() {
    Wire.begin(); 
    Serial.begin(115200); 

    }

void loop() {

  for(uint8_t i; i<6; i++){

Serial.println(mag_output_reg[i]);
Serial.println(mag._output_reg_array[i]);
Serial.println(" ");
Serial.println(mag_setting[i]);
Serial.println(mag._setting_array[i]);
  }

I expected to see:

0 0 3 3

16 16 4 4

1 1 5 5

32 32 6 6

2 2 7 7

0 0 8 8

but the code print out :

0 8 3 3

0 3 4 4

16 4 5 5

1 5 6 6

32 6 7 7

2 7 8 8

I can't understand why!
Please help! :slight_smile:
Thank you.

Regards.

    int _setting_array[];
    int _output_reg_array[];

Wrong. If you do not supply a size in the braces, the compiler will count the initializers to determine the size. Since you didn't supply any initializers, the array can hold zero elements.

      for(uint8_t i=0; i<6; i++)
      {
    _setting_array[i] = setting_array[i];
                                                                         
      }

So, when you try to copy 6 elements into your array that can hold zero, you just crapped all over memory you don't own.

You either need to have _setting_array be a pointer or you must give it a size that is large enough to hold however many elements you plan to copy to the array.

If it is a pointer, you do NOT copy anything. You simply make the pointer point to the array that was passed in.

ok. now it works!
Sorry for the stupid question.
Thankyou very much PaulS!

Sorry for the stupid question.

It wasn't a stupid question. It was a stupid mistake. 8)

It was actually a common mistake, which is why I spotted it so quickly.