Basic question about storing an object

Hey guys,

I'm a bit confused what's the best way to store my sensors name. I'd really appreciate any information about what I am misunderstanding. My problem is that I run into a memory issue and I try to improve the memory usage - so I thought lets begin with the strings. The name of the sensor is used to identify it in debug-messages.

With either option 1a or option 1b (no option 2, no option 3, no option 4) my Arduinos RAM is used with 9 Bytes (some default stuff), I'm fine with that.

  • The combination of 1a and 3 still uses 9 Bytes. I'm wondering why it is not 9 + 4x2B (for 2B/int) = 17B because it is in global scope?
  • Adding option 4 I was expecting 17B + 10x1 (1B/char) = 27B - but it's still 9B?
  • Adding option 2a results in 33B - why?! I still expected 27B.
  • Changing 1a to 1b results in 15B, but I expected either 9B (only for the default stuff) or 14B (for copying the value -> 9 + 4x1 (1B/char from "name") + 1B (for zero-terminating of "name") - why?
  • One more point: Changing now 2a to 2b still uses 15B (also 15B is the result when changing 2a to 2b in list-step 3) - I don't get it?
class cClass {
  public:
  
  cClass(){}
  
  void setName(const char* sensorName) {
    // Option 2a
    //strcpy(sName, sensorName);
    // Option 2b
    //EEPROM.put(0, sensorName);
  }
  const char sName[10];

  // Option 3
  int a;
  int b;
  int c;
  int d;

};

// Option 1a
 cClass newC;
void setup() {
  // Option 1b
  //cClass newC;
  newC.setName("name");
}

void loop() {
}

Many thanks in advance and best regards
Stefan

I'm having trouble understanding your issue, but I doubt that you're likely to have enough sensors that giving each of them a ten byte name is going to kill your RAM usage. Post your code so we can look for other places to save.

Thank you for your quick response!
If I'd post my complete code, I'd post about 3.000 lines of code - that's not the best way to search the problems, I think. :slight_smile: I'm beginning at the smallest / first point where my memory usage could be reduced - and that's the way to store my objects. But obviously I have problems understanding the way the storing works as you can see in my example code - I am expecting different value than those really come true at the end of compiling. To really solve my memory issue I need to find my misunderstanding of memory usage in general - do you agree with that? :confused:

But as I am very happy about every helpful support I give you the link to the Git instead of posting the code: https://github.com/alve89/growbox