Hey everybody,

I saw the thread for this in c++ but I want to know this for c language.

I want to write a program in c that lets me calculate the heap memory size available to malloc function as well as the amount of size currently allocated.

Can anyone guide me in the right direction...

I am using mingw on windows platform but I have no problems working on linux.

Thanks a lot.....

6 Years
Discussion Span
Last Post by griswolf

According to my reading, you cannot ask in a portable way for the amount of available or in-use heap. The best you can do in platform-independent C, which "best" is not really much, is attempt to (m)alloc some memory, and note if you get back a non-null pointer. C does not specify how the platform must handle heap memory (at least partly because C must work in a very wide variety of places, including firmware that doesn't have a proper OS at all). As a consequence, C also doesn't give you any way to ask about the heap's current status aside from the very crude probe I already mentioned.

The malloc() and calloc() functions return a pointer to the allocated memory if successful; otherwise a NULL pointer is returned and errno is set to ENOMEM.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.