Dynamic memory allocation - how is it supported?

I just learned a bit about C++, and there seems to be the C++ commands New and Delete, that is used for dynamic memory allocation. When I read the reference for the Arduino IDE, I do not find these possibilities. Am I right?

I read this link too about memory use of the Arduino microcontrollers:
https://docs.arduino.cc/learn/programming/memory-guide/

I can understand the reason for not implement New and Delete. However, as a programmer you may like to use the heap RAM Area anyway to some dynamic allocation, when you are careful about what you do. Perhaps somebody else can explain what might be helpful in that regard. Can you get an idea about the size and position of the heap RAM area?

Actually flash memory is used, and it could be possible to use that as well for some applications.

Arduino IDE uses a C+ language and all its methods. So the new() and delete() are supported too. But their use in microcontrollers is very controversial.
As for me - I used dynamic allocation very actively in my arduino code.

You might be interested in seeing a recent discussion:

Thanks - I am glad to know.
When I looked the reference here:
https://www.arduino.cc/reference/en/
I did not find new and delete, so therefore I assumed they were not there.

I will read your link too.

The Arduino Reference should not be considered an complete, authoritative source for all things C++. Not even all things Arduino for that matter.

2 Likes

This reference contains only Arduino-specific functions and methods. In addition to them, most standard С++/C language constructs are supported, although there are exceptions.
For example, the Arduino code for AVR controllers does not support most operations with float, for example printf, sprintf etc

1 Like

The Arduino toolchain uses a full C++ compiler (gcc), so for the most part everything is supported with some exceptions to save memory (No float support in sprintf for example).

The Arduino reference is basic and more focussed on the Arduino specific functions in its library, with a helping of things C++ you will find handy in Arduino coding. Use an actual C++ reference if you need more than this.

Thanks for the clarification about this reference. Are there some ways to get information about how much RAM heap space, that are left to be allocated?

Yes. Adafruit has a freeMem or freeMemory function that'll tell you.

Be careful with allocations on the smaller Arduino's - you're asking for trouble that'll be hard to diagnose.

1 Like

According to that, my Classic Nano has more that 2k RAM :rofl: See Classic Nano has too much RAM

1 Like

So you got one of the secret Nanos with extra RAM. They were a reward for faithful who bought genuine Arduinos - probably a collector’s item one day. Don’t blab it around :stuck_out_tongue:

I’m surprised that Lady Ada hasn’t fixed it since it’s apparently been known for a while. Maybe reach out to her to see if one of her crew could do the needful.

That was done around the date that I started the topic here. There is somewhere on their pages a way to report issues; it was not via their forum.

Thank you for the advise regarding the Adafruit freeMemory function.

Thanks for the link. Yes.

I am still a beginner in C++ and Arduino, and the link with the three/five/zero rule is somewhat above my level yet.

Yesterday I posted some debug recorder code, that I have used recently. It was a bit risky for me, because at my level of programming C++, it could easily make me look foolish:

https://forum.arduino.cc/t/debug-recorder-used-with-motor-control-loop/1215427

I would have liked the code to be placed in a library, so it could be used for more projects more easily, but then I realized, that it was not possible, because I need to transfer some constant definitions from the main code. I think the starter of your linked thread had realized the same problem.

When you want to separate the code in a nice way for a problem like this debug recorder try to solve, then I guess, that you need to allocate the memory at run time. Actually it do also open up more possibilities to record more data types and a nicer user interface to such a library.

I noticed recently, that some string possibilities seemed not present. According to this tutorial video: https://youtu.be/lTPT1cPfVmI you got:
.length
.empty
.clear
.append
.at
.substr
.insert
.find
.erase

I was not able to do that on strings. Perhaps I did something wrong.

The compiler allow me to define a pointer like this:

byte* ptr = &ADCSRA;

ADCSRA is hardware register in the ADC of the 328P chip. Can this kind of register be considered as memory mapped in the same way as RAM or Flash code memory?

Show the code that gave you problems.

Are you talking about the Arduino String class or the C++ std::string class?

The processor's datasheet will provide the memory map details.

BTW, the above is incorrect. It should be:

volatile byte* ptr = &ADCSRA;

Thanks. I just tried to download the code from Adafruit and I could repeat the same error and same successful correction.

I am not sure, what I talk about. The concept of class is still somewhat blurry for me, but it helped a bit when you say, that String is a class. But the above video showed several member functions, that was not found in the arduino String class definitions. So I guess this confused me. This is some code for arduino:

String str;
String str2;
int n;
char ch;

void setup() {
Serial.begin(9600);
str = "ABC";
Serial.println(str);
n = str.length();       // length() is OK
Serial.println( n );    
str.clear();            // clear() is not part of the Arduino String class
if (str.empty()) {Serial.println("string is empty");}; //empty() is not part of the Arduino String class
str.append('D');        // append() is not part of the Arduino String class
Serial.println(str);
ch = str.at(2);         // at() is not part of the Arduino String class
str2 = str.substring(0,1);  // substring() is OK
str.insert(1,"X");      // insert() is not part of the Arduino String class
n = str.find('B');      // find() is not part of the Arduino String class
str.erase(0,1);         // erase() is not part of the Arduino String class
}

void loop() {
delay(1);
}

The YouTuber referenced this webpage with a lot of member functions to a string. Perhaps the names of these member functions are not that agreed upon among C++ compilers.

https://cplusplus.com/reference/string/string/

The linked video does not refer to the Arduino 'String' class but rather the C++ std::string class that you cited:

They are not the same thing.