I am a bit confused about how memory works in Arduino. I have created an integer array like so:

int* arr = new int[3] {1, 2, 3};

When I go to delete it:

delete[] arr;

The first element is deleted, and all the remaining ones are still present and accessible.

Serial.println(arr[0]); // prints 0
Serial.println(arr[1]); // prints 2
Serial.println(arr[2]); // prints 3

First of all: it is not a good idea to do dynamic memory maagement in embedded systems (in casu arduino)
Secondly freeing memory means: marking the memory as available. It does not mean "setting the memory to 0"
because ar still points to the same locaton in memory it is "normal" that it also still reads the old values. Only -according to the C CP0P0 pàrogramming rules- you should not do so. Reading will not cause problems but writing will very likely.


It is not a good idea to use dynamic memory in such a small devices in general.

And delete doesn't mean setting values to something at all. It's just returning allocated memory back ("to system" is not correct term here as there is no system).

You should try to use objects with some action in constructor and destructor (like printing values).

For example:

class test {
  test(int _i):i{_i} { Serial.print("test:"); Serial.println(i); } 
  ~test() { Serial.print("~test:"); Serial.println(i); } 
  int i;

void setup() {
  test * arr = new test[3] {4,6,9};
  delete[] arr;


void loop() {



Deleting newed memory only informs the system that you are no longer using it and releasing it for re-assignment. The pointer still points to memory containing your array but that memory is no longer reserved for you. You could, in theory, go on using it, but another new might well reassign it for another use and the data that was in it would likely get over-written.

It would be good form to zero your pointer (arr = (int *)0;). It accomplishes several things:

  • Your code clearly reminds you that the pointer is no longer usable;
  • Your code clearly informs the next reader that the pointer is no longer usable;
  • An attempt to use the pointer is quite likely to fail immediately (depends on the envrionment's ability to trap attempted accesses to location 0). The alternative is your code may work anyway - until someday that memory actually does get re-assigned and suddenly, code that has worked "forever", breaks, and in a non-obvious way. Save yourself or the guy or gal who follows you some grief, and make it a habit to zero your pointers once they are no longer valid.
  • Note that arr = 0; is sufficient - you do not need to add (int*). 0 is defined for pointers. – John Burger Jul 21 '16 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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