0

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?

4
  • Yes, you can use 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".
    – Majenko
    Commented Jan 20, 2019 at 13:37
  • @Majenko So I will lose the data when Arduino is powered off right?
    – Stel Team
    Commented Jan 20, 2019 at 13:39
  • Yes. Like you lose your word document when you unplug the power from your computer.
    – Majenko
    Commented Jan 20, 2019 at 13:41
  • @Majenko Thanks! It will be great if you can post that as answer.
    – Stel Team
    Commented Jan 20, 2019 at 14:00

1 Answer 1

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.