help on String class usage

Hello,

I am have to continue someone else's arduino code. This code sometimes seem to randomly produces behavior that i suspect is due to the arduino resetting itself, which could be caused by memory overflow.

Reading through the code, I can see that

  1. the guy made an extensive usage of the arduino String class
  2. it looks like the code of someone who is used to python on PC where memory is often overlooked. i see many opportunities for memory overflow.

Reading through the official String class documentation, i struggle to find there a link towards actual implementation in order to understand how the memory allocation is being done. That reminds me a lot garbage collections nightmares caused by abuse of String objects in java back in the 90s :smiley:

I would rather get rid of String entirely, but if i go that path, i would have to rewrite the entire program + a thrid party lib. Besides, maybe the behavior has nothing to deal with String object and i may write an even poorer code myself anyway.

My questions

  1. in general when i read the arduino documentation, where can i find the corresponding implementation source code?
  2. in particular, where is the implementation for the arduino String class?

thanks

Here is the source for the ESP version:

The main problem is heap storage fragmentation. You can mitigate this to some extent by using the reserve method of the String class.
C/C++ can not have a garbage collector or any sort of heap defragmentation utility because the user is free to keep his own pointers into the heap which would impossible for any such utility to maintain. Also, for real-time systems, asynchronous tasks blocking while cleaning up storage, would be unwelcome.

You don't mention which Arduino. Search your computer for WString.h/WString.cpp (not sure about the capitalisation).

fourchette:
My questions

  1. in general when i read the arduino documentation, where can i find the corresponding implementation source code?
  2. in particular, where is the implementation for the arduino String class?

As suggested by sterretje's reply, the String class implementation is in the Arduino core library. For this reason, the location of the source code depends on which board you are compiling for. For example, different source code will be used if you're compiling for an Uno than it would if you're compiling for a MKR Zero.

The easiest way to find the active hardware package location is as follows:

  • Select a board from the hardware package you want to find from the Tools > Board menu
  • File > Examples > SPI > BarometricPressureSensor (or any other SPI example sketch)
  • Sketch > Show Sketch Folder
  • Move up folder levels until you reach the one that contains boards.txt
  • Open the cores/arduino folder. This is the location of the core library that defines the standard Arduino API for the boards of that hardware package. Although cores/arduino is the common folder name, you might find the "arduino" folder name is different in some 3rd party cores

The String class is in WString.h and WString.cpp files under that folder.

Arduino is in the process of moving the non-architecture specific code out of the individual core libraries and into a common repository (ArduinoCore-API) from which it can be shared by all hardware packages. They have selected the String class as one of the bits of the core library that can be moved to ArduinoCore-API. This has already been implemented in the Arduino megaAVR Boards and Arduino mbed Boards cores. For this reason, you may find the String class source code under cores/arduino/api/String.h and cores/arduino/api/String.cpp instead of the WString.h and WString.cpp directly under cores/arduino.