I am using the dynamic memory in AVR microcontroller, so How to know that there is collision between stack and heap or if the memory has been filled?
If malloc() returns a null pointer, it knows it's out of space. This doesn't catch the case of the stack hitting the heap - that you know has happened because it crashes based on how much memory has been used. I don't know if there's a better way.
Generally speaking, if you're using dynamic memory allocation on an AVR - first, do you really need it, or can you move to static allocation? If at all possible, avoid dynamic memory allocation. Secondly, if you must use dynamic memory allocation, you have to be very careful in your coding, because it's very easy to get yourself in trouble with dynamic memory allocation.
Try Stack Painting.
ahmednaoum: I am using the dynamic memory in AVR microcontroller, so How to know that there is collision between stack and heap or if the memory has been filled?
There is no reliable way to know. Often as not, if it happens, your program will crash. If not, some data will likely be corrupted. but it is simply not practical to know how, where, or when the corruption occurred. It is FAR easier to simply design the code in a way that the approximate maximum memory usage is limited to a safe value, leaving some margin.
Regards, Ray L.