Malloc -> how much memory has been allocated?

后端 未结 7 1281
梦谈多话
梦谈多话 2020-12-20 18:44
# include 
# include 
# include 
# include 

int main ()
{
  char * buffer;
  buffer = malloc (2);

          


        
7条回答
  •  天命终不由人
    2020-12-20 19:06

    There is no compiler/platform independent way of finding out how much memory malloc actually allocated. malloc will in general allocation slightly more than you ask it for see here:

    http://41j.com/blog/2011/09/finding-out-how-much-memory-was-allocated/

    On Linux you can use malloc_usable_size to find out how much memory you can use. On MacOS and other BSD platforms you can use malloc_size. The post linked above has complete examples of both these techniques.

提交回复
热议问题