malloc(2048) failied.

Hi everyone,
I am trying to debug a very simple code:
 
double* anArray= (double*) malloc(16*16*sizeof(double));

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.
Last edited on
Thanks Athat, but that doesn't help. I have been using malloc for ages, but this is the first time I get this error.
Perhaps you've run out of memory. Have you been freeing memory when you're done with it?

@kbw, He said if he allocates memory smaller or larger than that size, it successful. Only this size is giving error.

Could you post your code. May be something we can find.
Last edited on
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.

Thank you all.
Last edited on
That good.

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.
Hi kbw, that's true. Thanks for the note =)
closed account (zb0S216C)
Firstly, if the OS runs out of memory, it will automatically start page filing. Secondly, on line 3, you cast anArray back to a double, not a double*.
Topic archived. No new replies allowed.