On normal computers, heap will be cleaned as you close the program, but can I save data to Arduino's heap with malloc
function. And will it be cleaned after I power off the Arduino?
1 Answer
Yes, you can use malloc()
on the Arduino - however it is not something I would recommend you use since memory is at a premium and excessive use of malloc()
and free()
causes heap fragmentation which can eat your memory up and cause random crashes (String
is the biggest culprit of this).
I am not sure what you mean by "clean" the memory. When power is removed the content of the memory is lost. The whole of the SRAM forgets its contents if there is no power to maintain that state. A bit like when you unplug your computer it "forgets" that Windows is loaded.
If you want to retain information with the power removed you need to use the EEPROM.
malloc
(though it's seldom a good idea on such a small memory system). Will it get "cleaned"? No. It will get lost. There is no such thing as "cleaned". You remove the power the memory is erased. With no power to maintain the memory state there is no memory to "clean". And nothing to do that "cleaning".