The above statement seems to be perfectly fine to me, however, what I got was a NULL anArray.
Does any one have this problem before? I have tried several ways, which are essentially the same, but still cannot get it right:
1 2 3
void* anArray= malloc(2048);
or void* anArray= malloc(16*16*sizeof(double));
or double* anArray= (double)malloc(2048));
:-/
But if I use another size (could be smaller or larger e.g. malloc(1*sizeof(double))), it works.
Hi kbw and writetonsharma. Yes, it only happened when I try this size (maybe some other sizes too, but I tried 1 byte and 9999 bytes, it worked in both cases).
I fixed the bug. It is due to some memory corruption in another module (very unrelated to this one). The code itself is ok. I found it's very weird how 1 part of the program and affects others in a very random manner.
The modules aren't unrelated, they're using the same heap. If you corrupt it, it's state becomes invalid and the results of using it are non-deterministic.