Skip to content

The EEPROM::put() method's param doesn't support the type of String or a pointer #7251

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

Closed
5 of 6 tasks
mynameisbill2 opened this issue Apr 28, 2020 · 1 comment
Closed
5 of 6 tasks

Comments

@mynameisbill2
Copy link

mynameisbill2 commented Apr 28, 2020

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: [ESP8266]
  • Core Version: [Update EEPROM.cpp #6599]
  • Development Env: [Arduino IDE]
  • Operating System: [Windows]

Settings in IDE

  • Module: [Generic ESP8266 Module]
  • Flash Mode: [dio]
  • Flash Size: [4MB]
  • lwip Variant: [v2 Lower Memory]
  • Reset Method: [nodemcu]
  • Flash Frequency: [40Mhz]
  • CPU Frequency: [80Mhz]
  • Upload Using: [SERIAL]
  • Upload Speed: [115200]

Problem Description

The EEPROM::put() method's param doesn't support the type of String or a pointer. The put() method used sizeof(T), But the sizeof(String)'s output is a const value of 12. So, when the String's length bigger than 12, it failed.

MCVE Sketch

#include <Arduino.h>
#include <EEPROM.h>

String apikey("el2bmjdq45ttqqfrxsy15fx3wyr313t8");

String read__byte_array(unsigned short addr, unsigned short length)                                   
{
  char temp[length];
  for (int i = 0; i < length; i++)
  {
    temp[i] = EEPROM.read(addr + i); //读取数据
  }
  return temp;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  
  Serial.printf("apikey: %d\n", sizeof(apikey));

  EEPROM.begin(64);

  EEPROM.put(0, apikey);

  EEPROM.end();
  Serial.println("network configruation successfuly saved in EEPROM");

  EEPROM.begin(64);

  String apikey_r = read__byte_array(0, 64);

  Serial.println("=====");
  Serial.println(apikey_r);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Debug Messages

apikey: 12
network configruation successfuly saved in EEPROM
=====
ゑ?/
@devyte
Copy link
Collaborator

devyte commented Apr 28, 2020

That is correct, String is an object that is not self contained, similar to STL containers. You have to serialize/deserialize such objects yourself into some contiguous memory block and pass the result to put().
Closing due to not an issue.

@devyte devyte closed this as completed Apr 28, 2020
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

2 participants