C ++: two pointers (presumably) to the same address. Changing the content of one of them does not change the content of the other

I am new to C ++ and arduino and I don't understand what's going on.

The problem is this:

I have the following variables:

char *_array;
char _data[2];

      

When I do: _array = data;

And then I change the contents _data, for example data[0] = 'C'

, data[1] = 'D'

. The contents of the _array do not change and I need to do it _array = _data

again in order to apply the changes.

They don't seem to point to the same address.

Below is a sample code, the third track should be for me "3CD"

instead "3AB"

, but that's not what's going on.

could you help me? I do not understand. Thank!

#include <SoftwareSerial.h>
class Base {
    public:
        Base() {;};
        void setArray(char* array) {_array = array;}
        char *getArray() {return _array;}
    private:
        char *_array;
};

class A : public Base{
    public:
        A() : Base() {;};
        A(char data1, char data2)
        : Base()
        {
            setData(data1, data2);
            setArray(_data);
        }
        void setData(char data1, char data2)
        {
            _data[0] = data1;
            _data[1] = data2;
        }
        char *getData() {return _data;};

    private:
        char _data[2];
};

A a;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  a = A('A', 'B'); // This sets _data to "AB" and _array will point to _data
  Serial.write('1');
  Serial.write(a.getData()[0]);
  Serial.write(a.getData()[1]); // This will print "1AB" (as expected)

  a.setData('C', 'D'); // Here, _data changes to "CD" but _array no
  Serial.write('2');
  Serial.write(a.getData()[0]);
  Serial.write(a.getData()[1]); // This will print "2CD" (as expected)

  Serial.write('3');
  Serial.write(a.getArray()[0]);
  Serial.write(a.getArray()[1]); // This will print "3AB" (WHY?!?!?!)

  Serial.write('4');
  a.setArray(a.getData()); // If I call this function, _array changes to "CD"
  Serial.write(a.getArray()[0]);
  Serial.write(a.getArray()[1]); //This will print "4CD" (WHY I need to call setArray?)

  delay(3000);
}

      

+3


source to share


2 answers


a = A('A', 'B'); // This sets _data to "AB" and _array will point to _data

      

The above line A('A', 'B')

creates a new A and sets the inner one _array

. It then a = ...

calls the default destination, which simply copies each member from source to destination. Now a._array

points to a char array of the temporary and this is what the invalid results look like.



To avoid future mistakes,

  • implement the assignment operator
  • copy constructor
  • and also the default constructor initializes _array.
+6


source


You should at least write a copy constructor and an assignment operator for class A. A copy will copy the pointer by default and not redirect it to a new location.

eg.



A(A const& rhs) 
: Base(rhs)
{
    memcpy(_data, rhs._data, sizeof(_data));
    setArray(_data);
}


A& operator=(A const& rhs) 
{
    if (this != &rhs) {
        static_cast<Base&>(*this) = rhs;
        memcpy(_data, rhs._data, sizeof(_data));
        setArray(_data);
    }
    return *this;
}

      

0


source







All Articles