Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment Semantics Issue #2

Open
Lotharyx opened this issue Nov 3, 2023 · 1 comment
Open

Assignment Semantics Issue #2

Lotharyx opened this issue Nov 3, 2023 · 1 comment

Comments

@Lotharyx
Copy link

Lotharyx commented Nov 3, 2023

With intrinsic type T, given:

EEPROMStorage<T> first_thing(0, 0);
EEPROMStorage<T> second_thing((sizeof(T) + 1), 0);

Code such as the following:

...
second_thing = first_thing;
...

does not result in the value in first_thing being copied into second_thing, which misled me (an experienced coder) and probably would be very misleading to a novice (common among Arduino users). It in fact appears to cause the checksum of second_thing to become invalid, as evidenced by the constructor reinitializing the default value on the next boot.

Using a temporary variable yields the expected result, e.g.,

...
T temp = first_thing;
second_thing = temp;
...

I suggest that EEPROMStorage<T>::operator=(const EEPROMStorage & rhs) and EEPROMStorage<T>::operator=(const T& rhs) should be equivalent.

@Lotharyx
Copy link
Author

Lotharyx commented Nov 3, 2023

Here's an MCRE.

#include <EEPROM-Storage.h>

EEPROMStorage<int> a(0, 0);
EEPROMStorage<int> b(sizeof(int) + 1, 0);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Values at startup: ");
  Serial.print("a: "); Serial.println(a);
  Serial.print("b: "); Serial.println(b);
}

void loop() {
  // put your main code here, to run repeatedly:
  a++;
  if(a % 5 == 0) {
    b = a;
    Serial.println("Assigned a to b!");
    Serial.println("New values: ");
    Serial.print("a: "); Serial.println(a);
    Serial.print("b: "); Serial.println(b);
    Serial.println("Reset the arduino now.");
    while(true) delay(100);
  }
}

Actual output:

Values at startup: 
a: 0
b: 0
Assigned a to b!
New values: 
a: 5
b: 5
Reset the arduino now.
Values at startup: 
a: 5
b: 0
Assigned a to b!
New values: 
a: 10
b: 10
Reset the arduino now.

Notice that b is reinitialized to 0.

Expected output:

Values at startup: 
a: 0
b: 0
Assigned a to b!
New values: 
a: 5
b: 5
Reset the arduino now.
Values at startup: 
a: 5
b: 5
Assigned a to b!
New values: 
a: 10
b: 10
Reset the arduino now.

porrey added a commit that referenced this issue Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant