One little nasty assertion in the GNU standard library appeared a few days ago, kind out of nowhere, reporting “REENT malloc succeeded”:
Obviously it was caused by the call to srand() which sets the ‘seed’ for the standard library (pseudo) random number generator. The assertion happens as well later for calling the rand() function.
The random numbers are used for a little ‘asteroids’ game used in one of my classes (“Advanced Embedded Systems”):
Interestingly, the assertion for srand() occurred only in a few projects, but not in others. In my case I used NewLib-nano with as in MCUXpresso 11.6.x.
There were several reports in different forums about this assert and message, but none of the proposed solutions did make sense to me
“try to add this line of code “srand(time(NULL));” before calling rand() function, it worked for me.” (link)
nor worked in my case.
But some more investigations turned out that it has to do something with memory allocation, more precise with the heap and malloc().
And indeed, the projects which failed had the heap size set to zero (see “How to make sure no Dynamic Memory is used“):
Changing that to >0 (e.g. 1 KByte), and the assertion disappeared :-).
With this I have a workaround, but I’m still worried by the fact that the library random number generator needs some heap memory.
But in case you encounter the same assertion, I hope this article helps you out.
Happy de-asserting 🙂