How to check the memory allocated using malloc function.
How to check the memory allocated to mem.Code:int *mem;
mem=(int *)malloc(sizeof(int));
Printable View
How to check the memory allocated using malloc function.
How to check the memory allocated to mem.Code:int *mem;
mem=(int *)malloc(sizeof(int));
The size you mean? There's no portable way to do that. Each library implementation is free to add a function to get the size, but is not required to.
Better to keep check of the size manually.
How to check it manually?
Just do it theoretically, int is 4 bytes. The rest is easy.
The actual size is not guaranteed to be "just" 4 bytes. It may be more. But as I mentioned, there's no "100% guaranteed" way to check it. It differs from each implementation.
What exactly do you want to "check"? There are only two valid return values from malloc(): NULL for a failed allocation, and a valid pointer to a block of the size you requested, which is ANY other value than NULL. [Assuming of course we are talking about a malloc() that is implemented by a normal system library, rather than "I built my own malloc function, and I want to check if it works"].
--
Mats