1

I tried the following example that uses uint8_t.

Is using malloc() and free() a really bad idea on Arduino?

When I replaced it with uint16_t it stops running after five loops. Any ideas?

Leo

  • 2
    When you replaced what with what how when why? In other words, please show us your code, not some other code that is different to what you are having problems with! – Majenko May 3 '16 at 10:27
  • 1
    Chances are when you changed the type you either introduced or exposed a memory leak that killed the program. Please post the code and then we can see. – Code Gorilla May 3 '16 at 10:54
  • It is hard/impossible to debug issues with code that we can't see. – Nick Gammon May 4 '16 at 8:41
1

I suspect you changed everything to uint16_t but did not change the malloc call to account for the 2-byte size. Original:

_tester = (uint16_t *)malloc(len);

Try changing it to

_tester = (uint16_t *)malloc(len * sizeof(uint16_t));

This will multiply len by 2 to ensure it allocates enough space for all of the data.

(Beware that if you also changed len to be uint16_t then some of those random numbers from 1..1000 will be large enough to deplete all of the memory on an Uno. But at least it doesn't seem to crash.)

| improve this answer | |

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