You are probably running into a memory fragmentation problem due to low memory on your board. An easy fix would be working with a static array instead “CMPLEX Output[1600]”.
Can you post the complete FFT2D function? That way we can see the malloc in it’s context. I find it very strange that you allocate 25600 bytes instead of 1600 * sizeof(COMPLEX). Also strange why you would not check the returned pointer against NULL before proceeding.
Allocating 100 bytes and casting them to a COMPLEX array will give you an array of 6.25 COMPLEX elements. So if you want 100 COMPLEX elements in your array, you should do
It is possible that the code is going out of bounds because of that, which will mess up the heap desciptors of other allocated blocks and make the whole malloc/free functionality fail.
When calling malloc() a part of the heap will be reserved, and the underlying memory manager will add a struct with heap description (containing the size of allocated block) just before the pointer it returns. So if you allocate 2 buffers, the heap will look like this:
Then the FFT2D tries to allocate another 400kB of RAM, that’s already 800kB of RAM, and RLP only has 1MB available for code and data. So it is very likely that the first malloc call already fails.
Also consider using float instead of double if your application doesn’t require high precision, because the EMX module used on the FEZ Spider doesn’t have an FPU.
I agree that the code should not crash the board. If malloc couldn’t allocate the memory block, it should return NULL, and calling free on NULL should do nothing.
So I suggest you further simplify the code so GHI is able to reproduce it on their end.
@ WouterH - In a perfect world or any language run time library other than C you are correct. This has been an issue for as long as malloc and free have existed. free makes the assumption that you as the programmer are smart enough to check that the pointer you are supplying is valid and pointing to malloced memory. Failure to do so will result in undefined behavior.
So I would suggest that when doing RLP, you check the value returned by malloc and ensure that you don’t try to free anything other that a successfully supplied pointer from malloc.
edit:
The latest standard, WG14/N1124 , does say that if the value is NULL no action is taken. so if the compiler matches the version of the standard I just looked at, then your assertion is correct.